Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
禁用wpf DataGrid控件的内置降序排序_Wpf_Datagrid - Fatal编程技术网

禁用wpf DataGrid控件的内置降序排序

禁用wpf DataGrid控件的内置降序排序,wpf,datagrid,Wpf,Datagrid,我使用的是WPF数据网格,需要让用户只按升序方向对列进行排序,而不允许按降序方向进行排序 有没有一个简单的方法 简单的方法是使用集合视图源代码实现我自己的排序,并监听列标题上的鼠标单击事件。好的,我知道了 只需处理DataGrid的排序事件: private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e) { e.Column.SortDirection = ListSortDirec

我使用的是WPF数据网格,需要让用户只按升序方向对列进行排序,而不允许按降序方向进行排序

有没有一个简单的方法

简单的方法是使用集合视图源代码实现我自己的排序,并监听列标题上的鼠标单击事件。

好的,我知道了

只需处理DataGrid的排序事件:

 private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)
    {
        e.Column.SortDirection = ListSortDirection.Ascending;
    }

你也可以通过行为得到它。因为在这种情况下使用行为更好

这就是你要做的:

首先,将SortOnlyAscending.cs类添加到项目中

public class SortOnlyAscending:Behavior<DataGrid> 
{
    protected override void OnAttached()
    {
        AssociatedObject.Sorting += AssociatedObject_Sorting;
        base.OnAttached();
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Sorting -= AssociatedObject_Sorting;
        base.OnDetaching();
    }

    private void AssociatedObject_Sorting(object sender, DataGridSortingEventArgs e)
    {
        e.Column.SortDirection = ListSortDirection.Ascending;
    }
}
就这样。您还需要System.Windows.interactivity.dll来使用行为类。 您也可以从NUget软件包管理器下载它。这是

   <DataGrid>
        <i:Interaction.Behaviors>
           <local:SortOnlyAscending/>
        </i:Interaction.Behaviors>
   </DataGrid>
 xmlns:local ="clr-namespace:WpfApplication1" 
 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"