4

我有一个 ObservableCollection,它作为 ItemSource 绑定到我的 ComboBox。它是一个 ComboBox,显示您所做的更改。我现在的问题是,最新的更改显示在列表的底部。我试图在属性中反转它,但是当我返回时,ComboboxName.Reverse()我得到一个IEnumerable回来。我也不想用反转的数据进行第二次收集。

另一种方法是在 XAML 中解决这个问题。有人知道我该怎么做吗?

我在这里找到了这个答案,但我不明白如何实现它。 ObservableCollection 的逆序

4

1 回答 1

6

scm 代表

xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

有一个适合我的解决方案。您必须根据您的情况对其进行调整:

<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 引用,并将内容绑定到所需的属性:

 <ComboBox ItemsSource="{Binding Source={StaticResource customerGroups}}">
        <DataTemplate>
            <TextBlock Text="{Binding Path= FirstName}"></TextBlock>
        </DataTemplate>
    </ComboBox>
于 2013-02-01T09:52:57.473 回答