user3600403
user3600403

Reputation: 339

c# wpf listview sort by colum with listviewitems

i try to sort a wpf Listview, this works fine .. but only if i add new Items as simple Objects:

this works:

listview_files.Items.Add(new { isdir = (String)ele.Element("isdir"), number = (int)ele.Element("id"), name = (String)ele.Element("name"), size = groesse, right = modes, date = (String)ele.Element("date") });

with:

listview_files.Items.SortDescriptions.Add(new SortDescription(sortBy, direction));

but if i add the items like this:

ListViewItem myitem = new ListViewItem();
            myitem.Content=new { isdir = (String)ele.Element("isdir"), number = (int)ele.Element("id"), name = (String)ele.Element("name"), size = groesse, right = modes, date = (String)ele.Element("date") };
            myitem.ContextMenu = con2;
            myitem.MouseDoubleClick += myitem_MouseDoubleClick;

         listview_files.Items.Add(myitem);

it will allways sort to the same, there is no difference between sortdirection or clicked colum (Values of sortBy, direction are allways correct) I think the Problem is the "Content" Property, but how can i force the SortDescription to use the ListViewItem.Content Proberty for sorting?

Upvotes: 1

Views: 163

Answers (1)

Nadia Chibrikova
Nadia Chibrikova

Reputation: 5036

That's happening because the items collection' direct children are in the first case objects themselves and in the second - ListViewItem-s. So if you ask it to sort by number it won't find this property in a ListViewItem. It will, however, find Content.number and you may go with it. But why don't you use binding? It would be a more natural way to do it.

Upvotes: 1

Related Questions