- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我设置了一个 DataGrid
并将其绑定(bind)到一个 ICollectionView
,我向其中添加了一个组。
myView.Add(new PropertyGroupDescription("MyProp"));
然后添加一个简单的组样式:
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Element(s)"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
无论如何,组本身最初是按照我希望的顺序排列的。但是,当用户单击 DataGrid
中的标题以按该列排序时,它不仅对列,但它也按名称顺序对组进行排序。因此,如果我的组按 B、A、C 的顺序排列,它们将被排序为 A、B、C,然后对每个组中的列进行排序。
是否有一个简单的设置可以让列排序只对列排序而不对组排序。
需要明确的是,我不是在问如何禁用排序,而是在问如何在不按名称对组本身重新排序的情况下对组内的列进行排序。
最佳答案
我怀疑是否有任何简单的设置可以在组保持相同顺序时对数据进行排序,但是您可以定义自定义排序器并处理 DataGrid.Sorting
事件以根据所需顺序对数据进行排序。
假设 Category
是分组所基于的属性,我们有这样的东西:
MyList.Add(new ItemViewModel { ID = 1, Amount = 5, Category = "A" , ...});
// add some more items
GroupedList = new ListCollectionView(MyList);
GroupedList.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
然后你需要编写一个自定义排序器,根据你想要的组顺序作为主要排序参数对数据进行排序(以保持组顺序不变),然后按用户选择的列作为次要参数对数据进行排序(以对每个组内的数据进行排序) .
请注意,我写这篇文章时假设 Category
只能接受“A”、“B”和“C”,而且您想要的团体订单是 B-A-C,就像您提到的那样。
public class CustomSorter : IComparer
{
public ListSortDirection? Direction { get; set; }
public string SortMember { get; set; }
public CustomSorter(string sortMember, ListSortDirection? direction)
{
SortMember = sortMember;
Direction = direction;
}
// A custom comparer based on group header ("Category" in this example)
// in "B"<"A"<"C" order.
public int Compare(object x, object y)
{
var xVm = (ItemViewModel)x;
var yVm = (ItemViewModel)y;
if (xVm.Category == yVm.Category)
return CompareBySortMember(xVm,yVm);
if (xVm.Category == "B")
return -1;
if (xVm.Category == "A" && yVm.Category == "C")
return -1;
return 1;
}
// When two items have the same Category, comparison is made based on
// "SortMemberPath" property of the sorting column.
private int CompareBySortMember(ItemViewModel xVm, ItemViewModel yVm)
{
var xValue = xVm.GetType().GetProperty(SortMember).GetValue(xVm);
var yValue = yVm.GetType().GetProperty(SortMember).GetValue(yVm);
int result = xValue.ToString().CompareTo(yValue.ToString());
return Direction == ListSortDirection.Ascending ? result * -1 : result;
}
}
最后:
private void DataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
e.Handled = true;
// Set the sort order on the column
e.Column.SortDirection = (e.Column.SortDirection != ListSortDirection.Ascending)
? ListSortDirection.Ascending : ListSortDirection.Descending;
// Sort data based on predefined order of group headers and currently sorting column.
GroupedList.CustomSort = new CustomSorter(e.Column.SortMemberPath, e.Column.SortDirection);
}
关于c# - 如何禁用 Datagrid 中的排序组顺序,但保持组内列的排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403525/
我正在使用 Avalonia.Controls.DataGrid。默认情况下,当网格获得焦点并按下 Enter 时,它会自动处理事件并将选择移动到下一项。我怎样才能防止这种默认行为?我想要一个自定义的
根据我所见,Dojo DataGrid 中的格式化程序函数被赋予以下参数:单元格值、单元格行号和单元格对象本身。鉴于这些参数,您能否建议如何获取此单元格所引用的数据存储项?或者,如果您能提出替代方法,
我是DoJo开发的新手,所以这可能很基础。 我创建了一个EnhancedDatagrid,它可以很好地显示数据。 数据来自另一个页面中的JSON存储。 我有一个按钮,该按钮导致在数据存储中创建一个新条
我正在尝试在 WPF DataGrid 的 RowDetailsTemplate 中创建一个 DataGrid。 我有一系列工作。每个工作都可以分配一个或多个员工: public class Job
我有一个数据网格组件: [... some controlls ...]
我已经以编程方式创建了一个dojox.grid.datagrid,并且我需要对列进行自定义排序。为此,我尝试使用 ItemFileWriteStore.comparatorMap['field'] =
我试图在 UWP RadDataGrid 控件的 Telerik UI 中隐藏 GroupPanel。 radgrid.ShowGroupPanel 不起作用。 最佳答案 UserGroupMode=
我收到“在使用 ItemsSource 时操作无效。改为使用 ItemsControl.ItemsSource 访问和修改元素”错误,我不确定原因。 我正在尝试将数据网格嵌套在数据网格中。如果我只使用
我的 WPF forme Etudiant 中有一个 DataGrid 当单击 Etudiant 中名为 Epreuve 的行时,我需要显示其他数据网格,并且当在 Epreuve 中选择一行并且我需要
我有一个 DataGrid“嵌套”在另一个 DataGrid 的 RowDetailsTemplate 中。只要我的鼠标悬停在父 DataGrid 上行的主要部分上,滚动就可以正常工作,但是当鼠标悬停
我花了几个小时看这个没有结果。 我只是想要一个DataGrid X 列保持与 Grid 的相对宽度本身。 因此,例如: 第 1 栏:10% 第 2 栏:10% 第 3 栏:10% 我设置了一个附加到
我有 3 个共享相同数据类型的数据网格。我想配置一次列绑定(bind)并让 3 个数据网格共享资源。 例如
我之前关于检测 VM 中的属性更改的帖子不够深入,所以我发布了这个 我有一个工作网格。每个工作可以有一个或多个员工。 DataGrid 的 RowDetailsTemplate 包含另一个用于显示员工
我将尝试找出将 ComboBox 嵌入到 Flex (3.4) DataGrid 中的“正确”方法。根据权利(例如,根据本页 http://blog.flexmonkeypatches.com/200
我有一个对象集合,我们称之为 People,每个对象都由名称、ID 和时间字符串组成。这些人物必须显示在类似于固定行数和 9 列的网格上。这个想法是将人员作为行添加到第 1、2 和 3 列中的网格左侧
默认行为是使用 CTRL+Click 取消选择 Datagrid 中的项目 我希望能够通过鼠标单击(左键或右键)网格中的空白区域并让它取消选择任何选定的项目。 我已经用谷歌搜索死了,发现了一些非常复杂
我正在使用 DataGrid来自 WPF Toolkit我需要能够将注意力集中在网格的底部(即最后一行)。我现在遇到的问题是,随着行的添加,DataGrid 的滚动条不会随着新行的添加而滚动。实现此目
我正在使用 DataGrid 来管理服务配置。我想使用自动生成的行复选框来管理多个删除操作,但想使用 onRowClick 事件将行数据提供给模态对话框表单进行编辑。我的 onRowClick 处理程
我有一个包含项目的 DataGrid。当您右键单击其中一行时,会显示一个 Dojo 上下文菜单,其中包含删除该行的选项。如果您尝试右键单击 DataGrid 的空白区域,则不会显示上下文菜单……但是,
我想在另一个 Datagrid 的 RowDetailsTempalte 中使用一个 DataGrid。此内部 Datagrid 应将其列绑定(bind)到外部 Datagrid 中当前对象的属性。例
我是一名优秀的程序员,十分优秀!