Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do this is probably to add a SelectedBar property to your MyViewModel class and bind the SelectedItem property of the listboxes to that. This only allows one item to be selected at once, and provides you with something to bind your textbox to later.</p> <p>You can then set a binding (OneWayToSource) on the IsSelected property of each ListBoxItem (via ItemContainerStyle perhaps) to update the IsSelected property of each bar. To update the IsSelected property of the Foo objects, set a binding to the listbox's SelectedItem with a valueconverter to check if it is null.</p> <p>Edit:</p> <p>SelectedItem Property (implementing Dan's fix):</p> <pre><code>protected Bar selectedItem; public Bar SelectedItem{ get { return selectedItem; } set { selectedItem = null; NotifyPropertyChanged("SelectedItem"); selectedItem = value; NotifyPropertyChanged("SelectedItem"); } </code></pre> <p>ListBoxItem with Binding (assuming ListBoxItem DataContext is Bar viewmodel):</p> <pre><code>&lt;ListBoxItem IsSelected="{Binding Path=IsSelected, Mode=OneWayToSource}" /&gt; </code></pre> <p>Edit - fixes to your code:</p> <p>I managed to get your code working. Two issues I found: </p> <ol> <li><p>The reason items weren't appearing to select was that you'd re-templated the ListBoxItems populated with Bar objects, so there was no highlight style when an item was selected - fixed this by setting the ItemTemplate instead, which templates the content of the item rather than overriding the whole template.</p></li> <li><p>Instead of binding the SelectedItem of one of the nested ListBoxes to the SelectedItem index of the parent and then binding that to the viewmodel, I changed the binding to bind directly to the viewmodel, which fixed the multiple-selection issue.</p> <pre><code>&lt;ListBox x:Name="lbFoos" ItemsSource="{Binding Path=Foos}"&gt; &lt;!--Removed SelectedItem binding.--&gt; &lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="Foo: " /&gt; &lt;TextBlock Text="{Binding Path=Description}" /&gt; &lt;ListBox ItemsSource="{Binding Path=Bars}" SelectionChanged="ListBox_SelectionChanged" SelectedItem="{Binding Path=DataContext.SelectedBar, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox}}}"&gt;&lt;!--Changed binding to bind directly to ViewModel--&gt; &lt;ListBox.ItemTemplate&gt;&lt;!--Set ItemTemplated rather than ControlTemplate--&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType="ListBoxItem"&gt; &lt;Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWayToSource}" /&gt; &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; &lt;/ListBox&gt; &lt;/StackPanel&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; &lt;/ListBox&gt; </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload