C# - WPF在XAML中反向绑定ObservableCollection [英] C# - WPF Reverse binded ObservableCollection in XAML
问题描述
我有一个ObservableCollection作为ItemSource绑定到我的ComboBox。它是一个ComboBox,它显示您所做的更改。我的问题现在,最新的更改显示在列表的底部。
我试图在属性中反转,但是当我返回 ComboboxName.Reverse()
我得到一个 IEnumerable 回来。我也不想使用反转的数据做第二个集合。
I have a ObservableCollection which is binded as ItemSource to my ComboBox. It's a ComboBox which shows you your changes which you've done. My Problem is now, that the newest Change is displayed on the bottom of the list.
I tried to reverse it in the property, but when I return ComboboxName.Reverse()
I get an IEnumerable back. I also don't want to make a 2nd collection with the reversed data.
另一种方法是在XAML中解决这个问题。有人知道我该怎么做?
An other way would be to solve this in XAML. Does someone have an idea how I can do that?
我在这里找到了这个答案,但我不知道如何实现它。
ObservableCollection的反向顺序
I found this answer here, but i dont get it how i can implement it. Reverse order of ObservableCollection
推荐答案
scm代表
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
有一个适用于我的解决方案。您必须适应您的情况:
There a solution that works for me. You have to adapt it to your case:
<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>
然后,您将此资源引用为ComboBox的ItemsSource,并将内容绑定到所需的属性:
Then you reference this resource as ItemsSource of your ComboBox, and bind the content to the property you need:
<ComboBox ItemsSource="{Binding Source={StaticResource customerGroups}}">
<DataTemplate>
<TextBlock Text="{Binding Path= FirstName}"></TextBlock>
</DataTemplate>
</ComboBox>
这篇关于C# - WPF在XAML中反向绑定ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!