在WPF中的窗口数据文本中进行排序 [英] Sorting in window datacontext in WPF

查看:167
本文介绍了在WPF中的窗口数据文本中进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个有关的另一个问题。

Another question related to this one.

我有一个列表< SortableObjects> DataContext 我的MainWindow。我使用该列表来填充一个 ListBox 和一个 ComboBox 。当我对项目进行排序时,$ code> ComboBox 和 ListView 将被更新。但现在我需要以 ListView 不同的方式对 ComboBox 进行排序。 IE如果对象是一个Person,在 ComboBox 中,我需要使用LastName对它们进行排序,但在 ListView ,生日。如何实现这一点?

I have a List<SortableObjects> that is the DataContext of my MainWindow. I use that list to populate a ListBox and a ComboBox. When I sort the items, both the ComboBox and the ListView get updated all right. But now I need the ComboBox to be sorted in a different way than the ListView. I. E. If the object were a Person, in the ComboBox, I'd need to sort them by LastName, but in the ListView, by birthday. How can I achieve this?

谢谢!

推荐答案

a href =http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.aspx =nofollow noreferrer> CollectionViewSource ,您需要的每个单独订单:

Use CollectionViewSources for each of the separate orderings you want:

<UserControl.Resources>
    <CollectionViewSource x:Key="ComboBoxSource" Source="{Binding YourUnderlyingCollection}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="SomeProperty"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

    <CollectionViewSource x:Key="ListBoxSource" Source="{Binding YourUnderlyingCollection}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="SomeOtherProperty"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource ComboBoxSource}}"/>

<ListBox ItemsSource="{Binding Source={StaticResource ListBoxSource}}"/>

这篇关于在WPF中的窗口数据文本中进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆