DataGrid控件自动显示行号

来源:互联网 发布:动漫配音软件app 编辑:程序博客网 时间:2024/06/12 01:04

在项目中碰到了这个问题,在网上搜了半天,发现了一种解决方案,这种方法主要是在DataGrid加载Row时设定Row的行号,主要代码如下:

1 DataGrid.LoadingRow += (sender,e)=>
2 {
3   e.Row.Header = e.Row.GetIndex()+1;
4 };

但是在实际应用的过程中,发现一些问题,比如现在整个DataGrid的行都加载完了,现在DataGrid的行号是对的,但是,当从DataGrid中删除或是再增加一些行时,这时DataGrid的行号就显示错误了,主要原因的LoadingRow事件只是在Row加载时执行了一下,并没有在Row更新时再刷新行号,针对这个问题,我自己写了一段代码用来解决Row行号刷新的问题,由于DataGrid中并没有Rows这个属性,所以我们无法遍历DataGird的所有行对行号进行刷新(DataGrid的Row为DataGridRow类型,至少现在我还没发现怎么遍历,如果有发现的朋友可以指点指点),好吧,贴代码:

 1 public static class DataGridRowHelper
2 {
3 #region ShowRowIndex
4 public static readonly DependencyProperty ShowRowIndexProperty = DependencyProperty.RegisterAttached("ShowRowIndex", typeof(bool), typeof(DataGridRowHelper), new PropertyMetadata(false, new PropertyChangedCallback(OnShowRowIndexPropertyChanged)));
5 private static void OnShowRowIndexPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
6 {
7 if (sender is DataGrid)
8 {
9 DataGrid _dataGrid = sender as DataGrid;
10 _dataGrid.LoadingRow -= _dataGrid_LoadingRow;
11 _dataGrid.UnloadingRow -= _dataGrid_UnloadingRow;
12 bool newValue = (bool)e.NewValue;
13 if (newValue)
14 {
15 _dataGrid.LoadingRow += _dataGrid_LoadingRow;
16 _dataGrid.UnloadingRow += _dataGrid_UnloadingRow;
17 }
18 }
19 }
20 static void _dataGrid_UnloadingRow(object sender, DataGridRowEventArgs e)
21 {
22 List<DataGridRow> rows = GetRowsProperty(sender as DataGrid);
23 if (rows.Contains(e.Row))
24 rows.Remove(e.Row);
25 foreach (DataGridRow row in rows)
26 row.Header = row.GetIndex() + 1;
27 }
28 static void _dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
29 {
30 List<DataGridRow> rows = GetRowsProperty(sender as DataGrid);
31 if (!rows.Contains(e.Row))
32 rows.Add(e.Row);
33 foreach (DataGridRow row in rows)
34 row.Header = row.GetIndex() + 1;
35 }
36 public static bool GetShowRowIndexProperty(DependencyObject obj)
37 {
38 return (bool)obj.GetValue(ShowRowIndexProperty);
39 }
40 public static void SetShowRowIndexProperty(DependencyObject obj, bool value)
41 {
42 obj.SetValue(ShowRowIndexProperty, value);
43 }
44 #endregion
45
46 #region Rows
47 public static readonly DependencyProperty RowsProperty = DependencyProperty.RegisterAttached("Rows", typeof(List<DataGridRow>), typeof(DataGridRowHelper), new PropertyMetadata(new List<DataGridRow>()));
48
49 private static List<DataGridRow> GetRowsProperty(DependencyObject obj)
50 {
51 return (List<DataGridRow>)obj.GetValue(RowsProperty);
52 }
53 #endregion
54 }

自己写了一个类,其中有个ShowRowIndex的附加属性,用于DataGrid类型,还有一个Rows的附加属性,私有只读,类型为List<DataGridRow>,用于存贮DataGird的所有行,这样的话可以在DataGrid每次加载或卸载时都对DataGrid的行号进行更新。用法很简单,如下所示:

1 DataGridRowHelper.SetShowRowIndexProperty(NameOfDataGrid, true);
  • [WPF教程]【WPF读书笔记】动态资源扩展(DynamicResourceExtension)
  •  [WPF教程]WPF实例-记事本(下)
  •  [WPF教程]WPF实例-TextReader 记事本(上)
  •  [WPF教程]WPF Resource
  •  [WPF教程][WPF]Textbox与RichTextBox可输入半形字码
  •  [WPF教程][WPF]Image 與 Base64互相轉換
  •  [WPF教程]WPF Page页调用、触发Window的实现
  • [WPF教程]WPF/Winform中修改WebBrowser控件User-Agent的方法
  •  [WPF教程]WPF中控件绑定XML文件,实现修改XML文件即可以改变程序控件内容
  •  [WPF教程]WPF DataGrid实现为某个单元格赋值、获取某列的和、积等
  •  [WPF教程]WPF 资源(Resource)使用原则
  •  [WPF教程]WPF 多线程 更新UI 界面
原创粉丝点击