CollectionViewSource 动态排序

发布于 2024-12-23 18:21:34 字数 1308 浏览 0 评论 0原文

我有一个具有各种属性的 Car 类。

我有一个静态 Cars 集合类,为了简单起见,我在其中定义了一堆汽车项目,我可以通过对象数据提供程序在 XAML 中提供这些项目。

我有一个在 XAML 中定义的集合视图源,它按应有的方式成功绑定到我的 ObjectDataProvider。

我有一个显示集合的列表框。

我按照所有标准教程中的建议将排序添加到 CVS,并且一切正常。

我的问题: 假设我想在不同的字段上排序。当然有一种方法可以改变这一点,而无需向客户提供代码。所以我实现了一个组合框。

我使用以下代码将 Car 类中的属性列表加载到组合框中,但我不仅仅获得属性列表。我还得到了他们的数据类型。我不想要这个。

Car xyz=new Car(); //Make a temp Car Object so we can get a list of properties.
//Assign this to the combobox for listing.
cbxSortPrimary.ItemsSource = xyz.GetType().GetProperties();

结果(组合框中显示的内容):

System.String Model
Double Price
Int32 NoOfPrevOwners
DataType PropertyName
ect... ect...
ect... ect...
ect... ect...

我的目标是将属性名称加载到组合框中。然后使用选定的属性名称构建一行代码,如下所示:

myListBox.Items.SortDescriptions.Add(new SortDescription(ComboBox.SelectedItem.ToString(), ListSortDirection.Descending));

其中 ComboBox.SelectedItem.ToString() 将包含要排序的属性的名称。

那么如何去掉属性名称前面的数据类型我知道我可以将各种混乱的加载到另一个列表中,然后进行一堆字符串处理,查找右侧的第一个空格并砍掉之前的所有内容但肯定有一种更简单的方法。

实际上,我想做的是让用户对 Car 类的不同属性进行排序(因此我需要在某处加载这些属性并使它们可用供用户选择,因此是组合框)。我要问的是,必须有一种简单的方法来获取属性列表,而无需所有字符串操作代码,并且希望没有太多反思(除非我已经在不知情的情况下使用它),因为这似乎是一个非常基本的要求。

预先感谢您的任何帮助!

I have a Car class with various properties.

I have a static Cars collection class in which, for simplicity, I have defined a bunch of car items which I can make available in XAML via an object data provider.

I have a collection view source defined in XAML which binds successfully to my ObjectDataProvider as it should.

I have a listbox which shows the collection.

I added the sort to the CVS as recommended in all the standard tutorials and all works fine.

My Question:
Suppose I want to sort on a different field. Surely there is a way to change this without having to give the code to the customer. So I implemented a combo box.

I use the following code to load a list of properties from the Car class into the combo box but I don’t just get a list of properties. I also get their data types. I don’t want this.

Car xyz=new Car(); //Make a temp Car Object so we can get a list of properties.
//Assign this to the combobox for listing.
cbxSortPrimary.ItemsSource = xyz.GetType().GetProperties();

Result (what displays in the combo box):

System.String Model
Double Price
Int32 NoOfPrevOwners
DataType PropertyName
ect... ect...
ect... ect...
ect... ect...

My goal is to load in the property names to the combo box. Then use the selected property name to build a line of code like:

myListBox.Items.SortDescriptions.Add(new SortDescription(ComboBox.SelectedItem.ToString(), ListSortDirection.Descending));

Where the ComboBox.SelectedItem.ToString() will contain the name of the property to be sorted on.

So how do I get rid of the data type in front of the property name. I know I can do all sorts of messy loading into another list, then a bunch of string handling looking for the first space from the right and chopping everything before that. But surely there must be an easier way.

Effectively what I am looking to do is to let the user sort on a different property of the Car class (So I need to load the properties somewhere and make them available for the user to select, hence the combobox). What I am asking is that there must be an easy way to get the list of properties without all the string manipulation code, and hopefully without much reflection (unless I am already using it without knowing), as it seems like a very basic requirement.

Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2024-12-30 18:21:34

一点也不乱:

        var properties = new List<string>();

        foreach (var info in typeof(Car).GetProperties())
        {
            properties.Add(info.Name);
        }
        cbxSortPrimary.ItemsSource = properties;

It's not messy at all:

        var properties = new List<string>();

        foreach (var info in typeof(Car).GetProperties())
        {
            properties.Add(info.Name);
        }
        cbxSortPrimary.ItemsSource = properties;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文