绑定到列表框,但只显示所选元素? [英] Bind to a listbox, but only show the selected element?
问题描述
我有一个绑定到 ListBox 的对象集合,但实际上我只想显示所选元素,而不是整个集合.解决这个问题的最佳方法是什么?使用不同的控件?
I have a collection of objects which I bind to a ListBox, but I actually only want to display the selected element, and not the entire collection. What's the best way to go about this? Use a different control?
我想我可以做一个 Visibility ValueConverter 来检查 IsSelected 属性——如果没有被选中会崩溃……但我仍然对其他想法感兴趣.
I think I can do a Visibility ValueConverter which checks the IsSelected attribute -- and if not selected collapses... but I'm still interested in other ideas.
推荐答案
由于 ListBox
的全部目的是显示多个项目并为用户提供选择它们的方法,是的,我会使用不同的控件.
Since the entire purpose of a ListBox
is to display multiple items and provide the user with a way to select them, yes, I'd use a different control.
或者你可以这样做,这是进入愚蠢的领域:
Or you could do this, which is getting into the territory of stupid:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="false">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
这篇关于绑定到列表框,但只显示所选元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!