【问题标题】:C# - WPF Reverse binded ObservableCollection in XAMLC# - XAML 中的 WPF 反向绑定 ObservableCollection
【发布时间】:2013-02-01 09:29:32
【问题描述】:

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

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

我在这里找到了这个答案,但我不明白如何实现它。 Reverse order of ObservableCollection

【问题讨论】:

  • 链接答案有什么问题?什么不工作?
  • 当我使用 ObservableCollection 绑定而不是 {Binding Path=DocProps} 时,ComboBox 中没有任何内容。我现在也不知道scm: 代表什么。

标签: wpf xaml combobox observablecollection reverse


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多