1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I meant data binding (using ItemsSourse property), so you won't need to create each individual ListViewItem.
Thanks for your answer Nadia, "Content."+sortBy works fine =) i tryed Databinding with Itemsource (generating a List with ListViewitems, but then other things didnt work anymore, for example the Doubleclikevent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.