如果我有一个未排序的集合,是否有一种简单的方法来对其进行绑定和排序。我想用 XAML 来做(没有 Linq,没有 C#)
如果我的 DataContext 有一个属性,例如 MyItems,则很容易对其进行绑定:
<ListBox ItemsSource={Binding MyItems}/>
不过,我也想对其进行排序。使用 CollectionViewSource 应该是解决方案,但它对我不起作用:
<ListBox>
<ListBox.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source={Binding MyItems}/>
</Binding.Source>
</Binding>
</ListBox.ItemsSource>
</ListBox>
此时,我的列表框丢失了它的元素。
我错过了一些明显的事情吗?
您可以定义CollectionViewSource
作为资源并提供您想要的排序...
<Window.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding MyItems}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyItemName" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource cvs}}"/>
</Grid>
The scm
命名空间是xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)