c# - wpf DataGrid 列索引未保存

标签 c# wpf datagrid

我开发了一个应用程序来使用 WPF 隐藏/取消隐藏和重新排序数据网格中的列。

在列的重新排序功能中,我按以下方式更改索引。

foreach (var item in dataGrid.Columns)
{
     item.DisplayIndex = somenumber;
}

更改反射(reflect)在 View 中。但是,如果我尝试再次读取索引,则会显示原始索引,即 myDataGrid.Columns[0].Header 显示原始值而不是更改后的值。如何重新排序数据网格索引?

最佳答案

DisplayIndex 与 ColumnIndex 不同。 因此,当您更改 DisplayIndex 时,您只是在更改列的顺序,而不是其列数组中的基本索引。

实际上您无法替换 DataGridColumn 索引,但如果您坚持要清除 DataGridColumns 并根据需要以编程方式重新添加它们。 主要原因是 DataGridColumn“必须”属于 DataGrid 父级。

关于c# - wpf DataGrid 列索引未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27716066/

相关文章:

c# - 如何根据 C# 中字符串中元素的出现对数组进行排序?

c# - 如何删除应用程序中启动的所有线程?

c# - FFMediaElement : How to stream from CamLink 4K?

c# - 通过文本框 WPF 在 DataGrid 中搜索

c# - 从匿名对象中获取值

c# - 如何将样式写入 WPF DataGridColumnHeader

c# - 汇总所有节点

c# - WPF:当 DependencyProperty 更改时运行代码

wpf - 将形状添加到 Canvas 。如何绑定(bind)到虚拟机

WPF 多线程 : Using Dispatcher but the UI still hangs?