Snap to list but show only selected item?

I have a set of objects that I bind to a ListBox, but actually want to display only the selected item, not the entire collection. What is the best way to do this? Use a different control?

I think I can make a Visibility ValueConverter that checks the IsSelected attribute, and if it is not selected, collapses ... but I'm still interested in other ideas.

+4
source share
6 answers

Since the purpose of the ListBox is to display multiple items and provide the user with a way to select them, yes, I would use a different control.

Or you could do it that falls into the territory of stupidity:

 <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Style.Triggers> <Trigger Property="IsSelected" Value="false"> <Setter Property="Visibility" Value="Collapsed"/> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> 
+7
source

While I agree with Anders' answer, there is a way to show only the selected item in the ListBox , if for some reason, beyond my imagination, really what you want to do is:

 <ListBox> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Style.Triggers> <Trigger Property="IsSelected" Value="False"> <Setter Property="Visibility" Value="Collapsed" /> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> </ListBox> 
+3
source

You can get WPF to support the "current item" for you without using a ListBox. In fact, if I read this blog post correctly, it automatically does this when you set the DataContext to the collection.

You can reference the "current collection item" with a slash in the Path expression.

Since you already wrote your own β€œNext” and β€œPrevious” buttons (which, presumably, should already be connected to this mechanism of the current element), you can get rid of the insanity of one element in a-timeBox and just bind a TextBlock (or something else ) to the properties of the current element:

 <TextBlock Text="{Binding /ItemText}"/> 
+3
source

Use a text box. On your ViewModel (which I suppose you are binding to) create a property that displays the selected item (make sure INotifyPropertyChanged implemented) and bind the text box to this property.

+2
source

I tried something similar and came up with this neat solution:

 <Expander Header="Elements" Name="FooExpander"> <Expander.HeaderTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding}"/> <ListBoxItem Content="{Binding ElementName=ElementList, Path=SelectedItem}"> <ListBoxItem.Style> <Style TargetType="ListBoxItem"> <Style.Triggers> <Trigger Property="Content" Value=""> <Setter Property="Visibility" Value="Collapsed"/> </Trigger> </Style.Triggers> </Style> </ListBoxItem.Style> </ListBoxItem> </StackPanel> </DataTemplate> </Expander.HeaderTemplate> <ListBox x:Name="ElementList" SelectionChanged="SelectionChanged" ItemsSource="{Binding Path=Foo}" DisplayMemberPath="Name" /> </Expander> 

 private void SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { FooExpander.IsExpanded = ((sender as ListBox).SelectedIndex == -1); } 

Maybe you find it inspiring.

+1
source

Use the combo box instead of the list. Only the selected item is displayed in the combo box, but you can still select an item from the entire list. Alternatively, you can use up / down to scroll through the list.

0
source

All Articles