c# - Implementing Initial sort on ListView using a Class -


i used class found @ http://www.thomaslevesque.com/2009/08/04/wpf-automatically-sort-a-gridview-continued/. doesn't provide ability sort listview columns when application loads. need click columns before works. knowledge hasn't advanced enough able implement this. can me this?

from following ... http://www.wpf-tutorial.com/listview-control/listview-sorting/

you can add sortdescriptions listview's collectionview, ...

collectionview cv = (collectionview)collectionviewsource.getdefaultview(yourlistviewname.itemssource); cv.sortdescriptions.add(new sortdescription("columnnametosort", listsortdirection.ascending)); 

so, if listview called lv, , want sort name have ...

collectionview cv = (collectionview)collectionviewsource.getdefaultview(lv.itemssource); cv.sortdescriptions.add(new sortdescription("name", listsortdirection.ascending)); 

in code linked to, there 2 functions

private static void addsortglyph private static void removesortglyph 

i don't see reason why can't call addsortglyph manually after add sortdescription code.


in fact there public static void applysort function ... call instead of adding code first suggested!


getting columns can done using method described here ... get gridviewcolumn header value listview? ... must done after window has been activated (because headers don't exist during window's constructor).

ultimately, add following code mainwindow.xaml.cs

    private void window_activated(object sender, eventargs e)     {         list<gridviewcolumnheader> headers = getvisualchildren<gridviewcolumnheader>(downloadlist).tolist();         gridviewsort.applysort(downloadlist.items, "file name", downloadlist, headers[10]);     }      public static ienumerable<t> getvisualchildren<t>(dependencyobject parent) t : dependencyobject     {         int childrencount = visualtreehelper.getchildrencount(parent);         (int = 0; < childrencount; i++)         {             dependencyobject child = visualtreehelper.getchild(parent, i);             if (child t)                 yield return (t)child;              foreach (var descendant in getvisualchildren<t>(child))                 yield return descendant;         }     } 

and add window xaml element

activated="window_activated" 

eg

<window x:class="mackerel_download_manager.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:p="clr-namespace:mackerel_download_manager.properties"     xmlns:util="clr-namespace:wpf.util"     title="mackerel download manager" height="auto" width="auto" activated="window_activated"> 

i tested on code ... easier guessing i've been doing!


Comments