简体   繁体   English

对xaml中的第一列启用DataGrid排序

[英]Enable DataGrid sorting for first column in xaml

I have a DataGrid in WPF with autogenerated columns. 我在WPF中有一个带有自动生成列的DataGrid。

How can I disable sorting functionality of all the rows except the first one which corresponds time in my Source. 如何禁用除与Source中的时间相对应的第一行以外的所有行的排序功能。

I am following MVVM pattern and I know that CanUserSortColumns is disabling sorting for all the columns. 我正在关注MVVM模式,并且我知道CanUserSortColumns正在禁用所有列的排序。 I want to disable all but the first column. 我想禁用除第一列以外的所有内容。

Should I write trigger or interaction or something else? 我应该编写触发器或交互或其他内容吗? All the help is appreciated. 感谢所有帮助。

<DataGrid AutoGenerateColumns="True" 
        ItemsSource="{Binding MyDataTable}" 
        CanUserSortColumns="False">
</DataGrid>

I found this code snippet for you: 我为您找到了以下代码片段

<my:DataGridTemplateColumn  SortMemberPath="CompleteDate" Header="Complete Date" CanUserSort="true">
            <my:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <Binding Path="CompleteDate" ConverterCulture="en-GB" StringFormat="{}{0:MM/dd/yyyy}"/>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>

Use the CanUserSort Property to define it's sortable-state column explicit. 使用CanUserSort属性明确定义其可排序状态列。 The SortMemberPath defines the Property you use for sorting. SortMemberPath定义用于排序的属性。 Hope this helps. 希望这可以帮助。

UPDATE: If you are using autogenerated columns you can't access them via xaml. 更新:如果您正在使用自动生成的列,则无法通过xaml访问它们。 So you need to access them in code File. 因此,您需要在代码文件中访问它们。

I'm not familiar with the xaml Grid but would expect something like: 我对xaml网格不熟悉,但是会期望像这样:

//Bound Data here so that the Grid generate the columns

int i = 0;
foreach (DataColumn column in myGrid.ColumnCollection)
{
   if (i == 0)
      column.CanSortUser = true;
   else
      column.CanSortUser = false;

   i++;
}

The original Typenames can differ but something this way should be possible. 原始的Typename可以不同,但​​是应该可以这样做。

UPDATE-2 UPDATE-2

If you don't want to hurt the MVVM you can use this . 如果您不想损害MVVM,可以使用此方法 There is described how to use Interfaces to access the code and stay independet with your view and viewmodel. 描述了如何使用接口访问代码并与视图和视图模型保持独立。

The problem is, that DataGridColumnHeader has the CanUserSort property as a local computed property. 问题是, DataGridColumnHeader具有CanUserSort属性作为本地计算属性。 It can been set by manually generating the columns (what you don't want). 可以通过手动生成列(不需要的列)进行设置。

If it was a property with setter you could create a DataTrigger looking at TabIndex == 0 and set through Setter in xaml Style. 如果它是setter的属性,则可以创建一个DataTrigger查看TabIndex == 0并通过xaml样式的Setter进行设置。

Your only chance by using AutoGeneratedColumns is the following: 使用AutoGeneratedColumns唯一机会如下:

<DataGrid AutoGenerateColumns="True" 
    ItemsSource="{Binding MyDataTable}" 
    CanUserSortColumns="True"
    AutoGeneratedColumns="DataGrid_OnAutoGeneratedColumns">
</DataGrid>

your codebehind: 您的代码背后:

private void DataGrid_OnAutoGeneratedColumns(object sender, EventArgs e)
{
    DataGrid dg = sender as DataGrid;
    if (dg == null) return;
    dg.Columns.ToList().Select((col, indx) => new {Col = col, Indx = indx}).ToList().ForEach(obj => obj.Col.CanUserSort = obj.Indx == 0);
}

This enables the first columns UserCanSort property and disables the others. 这将启用第一列UserCanSort属性,并禁用其他属性。 This function is independend from your viewmodel and can been placed at your view.xaml.cs code behind file. 该函数独立于您的viewmodel ,可以放在文件后面的view.xaml.cs代码中。

EDIT: 编辑:

If you won't have any codebehind you can just do it by using your own DataGridControl . 如果您没有任何代码隐藏,则可以使用自己的DataGridControl

MyDataGrid.cs MyDataGrid.cs

namespace YourNamespace {
    public class MyDataGrid : DataGrid
    {
        protected override void OnAutoGeneratedColumns(EventArgs e)
        {
            DataGrid dg = sender as DataGrid;
            if (dg == null) return;
            dg.Columns.ToList().Select((col, indx) => new {Col = col, Indx = indx}).ToList().ForEach(obj => obj.Col.CanUserSort = obj.Indx == 0);

            base.OnAutoGeneratedColumns(e);
        }
    }
}

your view: 您的看法:

<YourNamespace:MyDataGrid AutoGenerateColumns="True" 
    ItemsSource="{Binding MyDataTable}" 
    CanUserSortColumns="True">
</YourNamespace:MyDataGrid>

and you are done without a single line codebehind. 这样就完成了,无需任何单行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM