yan*_*ver 2 wpf xaml reverse combobox observablecollection
我有一个ObservableCollection,它作为ItemSource绑定到我的ComboBox.它是一个ComboBox,可以显示您所做的更改.我的问题是,最新的更改显示在列表的底部.我试图在属性中反转它,但是当我返回时,ComboboxName.Reverse()我得到一个IEnumerable.我也不想用反向数据制作第二个集合.
另一种方法是在XAML中解决这个问题.有人知道我该怎么做吗?
我在这里找到了这个答案,但我不知道如何实现它. ObservableCollection的反向顺序
scm代表
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
Run Code Online (Sandbox Code Playgroud)
有一个适合我的解决方案.你必须根据你的情况调整它:
<Window.Resources>
<CollectionViewSource x:Key="customerGroups" Source="{Binding Path=Customers}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="IsCompany"></PropertyGroupDescription>
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="IsCompany" Direction="Descending"/>
<scm:SortDescription PropertyName="DisplayName" Direction="Ascending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
然后,您将此资源作为ComboBox的ItemsSource引用,并将内容绑定到您需要的属性:
<ComboBox ItemsSource="{Binding Source={StaticResource customerGroups}}">
<DataTemplate>
<TextBlock Text="{Binding Path= FirstName}"></TextBlock>
</DataTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)