WPF GridViewColumn Sort DataTemplate

wpf的GridViewColumn的排序要用到 ICollectionView   SortDescriptions .
SortDescriptions数组里是   SortDescription, SortDescription有2个参数, 第一个为属性, 第二个为升序降序的选择。

难点主要是第一个, 什么为属性? 属性就是你单条记录所绑定的数据层, 然后在里面选择你想要通过数据层的哪个字段来排序。


示例:

  <DataTemplate x:Key="isVisibleShown">
            < TextBlock Visibility ="{ Binding Value .IsShowMe, Converter ={ StaticResource BooleanToVisibilityConverter }}">
                < TextBlock.Text >
                    < PriorityBinding >
                        < Binding Path ="Value.Label" Converter ="{ StaticResource IsPropertyExistConverter }" IsAsync ="True"/>
                        < Binding Path ="Key" Converter ="{ StaticResource DebugConverter }" IsAsync ="True"/>
                    </ PriorityBinding >
                </ TextBlock.Text >            
            </ TextBlock >       
        </ DataTemplate >
        < GridView x : Key ="prGridView">
            < GridViewColumn HeaderTemplate ="{ StaticResource FieldCellTemplate }" CellTemplate ="{ StaticResource isVisibleShown }"/>
        </ GridView >

这个prGridView中column应用了一个DataTemplate, 假如 GridView绑定一个字典Dictinory<string, Model>, 那每条就是一个 KeyValuePair , DataTemplate里Text绑定了Model里面的Label, 
然后后台:

            ICollectionView view = CollectionViewSource . GetDefaultView ( lsvConfig . ItemsSource );

            if ( view . CanSort && view . SortDescriptions . Count <= 0)
            {
                view . SortDescriptions . Add ( new SortDescription ( "Value.Label" , ListSortDirection . Ascending ));
                view . Refresh ();
            }

指定按model里面的label来排序

你可能感兴趣的:(WPF)