【问题标题】:How to disable sorting on Group Column description field in Data grid?如何禁用数据网格中组列描述字段的排序?
【发布时间】:2020-05-11 15:18:43
【问题描述】:

我的 xaml 代码中有一个数据网格。我通过在我的视图模型中处理排序列更改事件来应用排序。还有一个单独的组列描述字段。

当我按列对网格进行排序时,调试器会根据我选择的顺序向我显示正确的结果,但是当分组列出现在屏幕上时,它会再次按分组列排序。

这是我的 xaml 代码:

xmlns:my="using:Syncfusion.UI.Xaml.Grid"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
xmlns:i="using:Microsoft.Xaml.Interactivity"
<my:SfDataGrid 
                    x:Name="ListReportForFilter" 
                    Margin="20,20,20,0"
                    GroupCaptionTextFormat="Tracking Number: {Key}" 
                    AutoExpandGroups="True"
                    ItemsSource="{Binding SearchReportPageFullList,Mode=TwoWay}"
                    Visibility="Visible"
                    AutoGenerateColumns="False"
                    BorderBrush="Transparent"
                    AllowFiltering="True"
                    AllowSorting="True"
                    ColumnSizer="Star" 
                    SelectionMode="None">
                    <i:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="FilterChanged">
                            <core:InvokeCommandAction Command="{Binding FilterChangedOnGrid}" />
                        </core:EventTriggerBehavior>
                        <core:EventTriggerBehavior EventName="SortColumnsChanged">
                            <core:InvokeCommandAction Command="{Binding SortingChangedOnGrid}" />
                        </core:EventTriggerBehavior>
                    </i:Interaction.Behaviors>
                    <my:SfDataGrid.GroupColumnDescriptions>
                        <my:GroupColumnDescription ColumnName="TrackingNumber"/>
                    </my:SfDataGrid.GroupColumnDescriptions>
                    <my:SfDataGrid.Columns>
                        <my:GridTextColumn  HeaderText="Recipient"
                                        TextAlignment="Center"
                                MappingName="Recipient"
                                            AllowBlankFilters="False"
                                 />
                        <my:GridTextColumn  HeaderText="Function"
                                MappingName="Function"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                 />
                        <my:GridTextColumn  HeaderText="Carrier"
                                MappingName="Carrier"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                 />
                        <my:GridTextColumn HeaderText="Tracking Number"
                                MappingName="TrackingNumber"
                                            IsHidden="True"
                                            TextAlignment="Center"
                                />
                        <my:GridTextColumn  HeaderText="Item Type"
                                MappingName="ItemType"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                 />
                        <my:GridTextColumn  HeaderText="Date"
                                MappingName="Date"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                 />
                        <my:GridTextColumn  HeaderText="Location"
                                MappingName="Location"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                />
                        <my:GridTextColumn  HeaderText="Duration (hrs)"
                                MappingName="Duration"
                                            AllowBlankFilters="False"
                                            TextAlignment="Center"
                                />
                    </my:SfDataGrid.Columns>
                </my:SfDataGrid>

这是我的视图模型代码与排序列更改事件:

public DelegateCommand<GridSortColumnsChangedEventArgs> SortingChangedOnGrid { get; }
SortingChangedOnGrid = new DelegateCommand<GridSortColumnsChangedEventArgs(SortingOnSFDataGridChanged);

private void SortingOnSFDataGridChanged(GridSortColumnsChangedEventArgs sortingdata)
    {
        try
        {
            var newSearchtotalFullList = new List<UndeliveredReportModel>();
            var columnNameSelected = sortingdata.AddedItems.FirstOrDefault().ColumnName;

            List<UndeliveredReportModel> totalDataAsPerFilterr = new List<UndeliveredReportModel>();
            switch (columnNameSelected)
            {
                case "Recipient":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Recipient).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Recipient).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "Function":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Function).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Function).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "ItemType":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.ItemType).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.ItemType).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "Carrier":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Carrier).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Carrier).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "Date":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Date).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Date).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "Location":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Location).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Location).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "TrackingNumber":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.TrackingNumber).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.TrackingNumber).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                case "Duration":
                    if (sortingdata.AddedItems.FirstOrDefault().SortDirection == Syncfusion.Data.ListSortDirection.Ascending)
                    {
                        //var asc = SearchReportPageFullList.OrderBy(x=>x.Duration).GroupBy(y => y.TrackingNumber).ToList();
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderBy(x => x.Duration).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
                    else
                    {
                        //var asc = SearchReportPageFullList.OrderByDescending(x => x.Duration).GroupBy(y => y.TrackingNumber).OrderBy(x => x.Key).ToList();
                        totalDataAsPerFilterr = SearchReportPageFullList.OrderByDescending(x => x.Duration).ToList();
                        newSearchtotalFullList = newSearchtotalFullList.Concat(totalDataAsPerFilterr).ToList();
                        break;
                    }
            }
SearchReportPageFullList = newSearchtotalFullList;
 }
catch (Exception ex)
        {

        }
}

我正在为此功能使用 Syncfusion UWP 工具,因为它符合我们的要求。

提前致谢。

【问题讨论】:

    标签: c# sorting xaml uwp syncfusion


    【解决方案1】:

    您的要求可以通过在 SfDataGrid 的 GroupColumnDescriptions 中将 SortGroupRecords 值设置为 False 来实现。请参考以下代码 sn-p 供您参考,

    <syncfusion:SfDataGrid.GroupColumnDescriptions>
       <syncfusion:GroupColumnDescription ColumnName="OrderID" SortGroupRecords="False" /> 
    </syncfusion:SfDataGrid.GroupColumnDescriptions>
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2016-03-13
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 2022-06-30
      相关资源
      最近更新 更多