经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
Windows Community Toolkit 4.0 - DataGrid - Part03
来源:cnblogs  作者:shaomeng  时间:2018/9/25 20:39:50  对本文有异议

概述

在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part02 中,我们针对 DataGrid 控件的 Utilities 部分做了详细分享。而在本篇,我们会对控件中最重要的 DataGrid 文件夹中的类做详细的分享。

下面是 Windows Community Toolkit Sample App 的示例截图和 code/doc 地址:

Windows Community Toolkit Doc - DataGrid

Windows Community Toolkit Source Code - DataGrid

Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls.DataGrid;

 

开发过程

DataGrid 文件夹中是 DataGrid 控件最重要的功能,首先我们还是先来看一下类结构:

包括了 Automation;DataGrid,DataGridColumn,DataGridRow,DataGridCell 控件实现,事件处理参数类和数据类等;

接着我们看几个重要的类和方法:

1. DataGrid.cs

这个类是 DataGrid 控件的主要处理类,功能也是比较复杂,单个类的代码行数是 9001 行,我们只挑两个方法来看一下。其他方法大家有兴趣或用到时可以在 DataGrid.cs 中查阅。

1) DataGrid()

首先看一下 DataGrid 类的构造方法,之所以看这个方法,是想让大家可以更了解 DataGrid 类中变量的初始化方式,这些变量在不同的交互场景下会被赋予不同的值。

  1. public DataGrid()
  2. {
  3. this.TabNavigation = KeyboardNavigationMode.Once;
  4. _loadedRows = new List<DataGridRow>();
  5. _lostFocusActions = new Queue<Action>();
  6. _selectedItems = new DataGridSelectedItemsCollection(this);
  7. _rowGroupHeaderPropertyNameAlternative = Properties.Resources.DefaultRowGroupHeaderPropertyNameAlternative;
  8. _rowGroupHeaderStyles = new ObservableCollection<Style>();
  9. _rowGroupHeaderStyles.CollectionChanged += RowGroupHeaderStyles_CollectionChanged;
  10. _rowGroupHeaderStylesOld = new List<Style>();
  11. this.RowGroupHeadersTable = new IndexToValueTable<DataGridRowGroupInfo>();
  12. _collapsedSlotsTable = new IndexToValueTable<Visibility>();
  13. _validationItems = new Dictionary<INotifyDataErrorInfo, string>();
  14. _validationResults = new List<ValidationResult>();
  15. _bindingValidationResults = new List<ValidationResult>();
  16. _propertyValidationResults = new List<ValidationResult>();
  17. _indeiValidationResults = new List<ValidationResult>();
  18. this.ColumnHeaderInteractionInfo = new DataGridColumnHeaderInteractionInfo();
  19. this.DisplayData = new DataGridDisplayData(this);
  20. this.ColumnsInternal = CreateColumnsInstance();
  21. this.RowHeightEstimate = DATAGRID_defaultRowHeight;
  22. this.RowDetailsHeightEstimate = 0;
  23. _rowHeaderDesiredWidth = 0;
  24. this.DataConnection = new DataGridDataConnection(this);
  25. _showDetailsTable = new IndexToValueTable<Visibility>();
  26. _focusInputDevice = FocusInputDeviceKind.None;
  27. _proposedScrollBarsState = ScrollBarVisualState.NoIndicator;
  28. _proposedScrollBarsSeparatorState = ScrollBarsSeparatorVisualState.SeparatorCollapsed;
  29. this.AnchorSlot = -1;
  30. _lastEstimatedRow = -1;
  31. _editingColumnIndex = -1;
  32. this.CurrentCellCoordinates = new DataGridCellCoordinates(-1, -1);
  33. this.RowGroupHeaderHeightEstimate = DATAGRID_defaultRowHeight;
  34. this.LastHandledKeyDown = VirtualKey.None;
  35. this.DefaultStyleKey = typeof(DataGrid);
  36. HookDataGridEvents();
  37. }

2) ShowScrollBars()

DataGrid 控件中滚动条的处理方法。如果 AreAllScrollBarsCollapsed 为 true,则按照该规则简单处理;如果为 false,先按照 mouse 和 touch 的类型进行判断处理,再根据 UI 设置里的 AreSettingsEnablingAnimations 和 AreSettingsAutoHidingScrollBars 属性来切换滚动条的状态,调用 SwitchScrollBarsVisualStates 方法。

  1. private void ShowScrollBars()
  2. {
  3. if (this.AreAllScrollBarsCollapsed)
  4. {
  5. _proposedScrollBarsState = ScrollBarVisualState.NoIndicator;
  6. _proposedScrollBarsSeparatorState = ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation;
  7. SwitchScrollBarsVisualStates(_proposedScrollBarsState, _proposedScrollBarsSeparatorState, false /*useTransitions*/);
  8. }
  9. else
  10. {
  11. if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsEnabled)
  12. {
  13. _hideScrollBarsTimer.Stop();
  14. _hideScrollBarsTimer.Start();
  15. }
  16. // Mouse indicators dominate if they are already showing or if we have set the flag to prefer them.
  17. if (_preferMouseIndicators || _showingMouseIndicators)
  18. {
  19. if (this.AreBothScrollBarsVisible && (_isPointerOverHorizontalScrollBar || _isPointerOverVerticalScrollBar))
  20. {
  21. _proposedScrollBarsState = ScrollBarVisualState.MouseIndicatorFull;
  22. }
  23. else
  24. {
  25. _proposedScrollBarsState = ScrollBarVisualState.MouseIndicator;
  26. }
  27. _showingMouseIndicators = true;
  28. }
  29. else
  30. {
  31. _proposedScrollBarsState = ScrollBarVisualState.TouchIndicator;
  32. }
  33. // Select the proper state for the scroll bars separator square within the GroupScrollBarsSeparator group:
  34. if (UISettingsHelper.AreSettingsEnablingAnimations)
  35. {
  36. // When OS animations are turned on, show the square when a scroll bar is shown unless the DataGrid is disabled, using an animation.
  37. _proposedScrollBarsSeparatorState =
  38. this.IsEnabled &&
  39. _proposedScrollBarsState == ScrollBarVisualState.MouseIndicatorFull ?
  40. ScrollBarsSeparatorVisualState.SeparatorExpanded : ScrollBarsSeparatorVisualState.SeparatorCollapsed;
  41. }
  42. else
  43. {
  44. // OS animations are turned off. Show or hide the square depending on the presence of a scroll bars, without an animation.
  45. // When the DataGrid is disabled, hide the square in sync with the scroll bar(s).
  46. if (_proposedScrollBarsState == ScrollBarVisualState.MouseIndicatorFull)
  47. {
  48. _proposedScrollBarsSeparatorState = this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpandedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed;
  49. }
  50. else
  51. {
  52. _proposedScrollBarsSeparatorState = this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed;
  53. }
  54. }
  55. if (!UISettingsHelper.AreSettingsAutoHidingScrollBars)
  56. {
  57. if (this.AreBothScrollBarsVisible)
  58. {
  59. if (UISettingsHelper.AreSettingsEnablingAnimations)
  60. {
  61. SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicatorFull, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpanded : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/);
  62. }
  63. else
  64. {
  65. SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicatorFull, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpandedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/);
  66. }
  67. }
  68. else
  69. {
  70. if (UISettingsHelper.AreSettingsEnablingAnimations)
  71. {
  72. SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicator, ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/);
  73. }
  74. else
  75. {
  76. SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicator, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/);
  77. }
  78. }
  79. }
  80. else
  81. {
  82. SwitchScrollBarsVisualStates(_proposedScrollBarsState, _proposedScrollBarsSeparatorState, true /*useTransitions*/);
  83. }
  84. }
  85. }

2. DataGridCellEditEndedEventArgs.cs

DataGrid 控件中有很多事件处理参数类,我们只看其中一个 DataGridCellEditEndedEventArgs 吧。很显然这个事件包含了 column row 和 editAction 三个变量,大家看到其他时间参数时,可以具体再分析。

  1. public class DataGridCellEditEndedEventArgs : EventArgs
  2. {
  3. public DataGridCellEditEndedEventArgs(DataGridColumn column, DataGridRow row, DataGridEditAction editAction)
  4. {
  5. this.Column = column;
  6. this.Row = row;
  7. this.EditAction = editAction;
  8. }
  9. public DataGridColumn Column
  10. {
  11. get;
  12. private set;
  13. }
  14. public DataGridEditAction EditAction
  15. {
  16. get;
  17. private set;
  18. }
  19. public DataGridRow Row
  20. {
  21. get;
  22. private set;
  23. }
  24. }

3. DataGridCellCollection.cs

DataGrid 控件中有很多数据类,我们看一个单元格集合类,可以看到集合中有 _cells,Count 变量,Insert 和 RemoveAt 方法等,处理逻辑都比较简单。

  1. internal class DataGridCellCollection
  2. {
  3. private List<DataGridCell> _cells;
  4. private DataGridRow _owningRow;
  5. internal event EventHandler<DataGridCellEventArgs> CellAdded;
  6. internal event EventHandler<DataGridCellEventArgs> CellRemoved;
  7. public DataGridCellCollection(DataGridRow owningRow)
  8. {
  9. _owningRow = owningRow;
  10. _cells = new List<DataGridCell>();
  11. }
  12. public int Count
  13. {
  14. get
  15. {
  16. return _cells.Count;
  17. }
  18. }
  19. public IEnumerator GetEnumerator()
  20. {
  21. return _cells.GetEnumerator();
  22. }
  23. public void Insert(int cellIndex, DataGridCell cell)
  24. {
  25. Debug.Assert(cellIndex >= 0 && cellIndex <= _cells.Count, "Expected cellIndex between 0 and _cells.Count inclusive.");
  26. Debug.Assert(cell != null, "Expected non-null cell.");
  27. cell.OwningRow = _owningRow;
  28. _cells.Insert(cellIndex, cell);
  29. if (CellAdded != null)
  30. {
  31. CellAdded(this, new DataGridCellEventArgs(cell));
  32. }
  33. }
  34. public void RemoveAt(int cellIndex)
  35. {
  36. DataGridCell dataGridCell = _cells[cellIndex];
  37. _cells.RemoveAt(cellIndex);
  38. dataGridCell.OwningRow = null;
  39. if (CellRemoved != null)
  40. {
  41. CellRemoved(this, new DataGridCellEventArgs(dataGridCell));
  42. }
  43. }
  44. public DataGridCell this[int index]
  45. {
  46. get
  47. {
  48. if (index < 0 || index >= _cells.Count)
  49. {
  50. throw DataGridError.DataGrid.ValueMustBeBetween("index", "Index", 0, true, _cells.Count, false);
  51. }
  52. return _cells[index];
  53. }
  54. }
  55. }

4. DataGridCell.cs

DataGrid 控件的单元格类,处理比较简单,我们通过构造方法来看一下类中都涉及到哪些事件的处理;可以看到,光标的一系列处理都有涉及。

  1. public DataGridCell()
  2. {
  3. this.IsTapEnabled = true;
  4. this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(DataGridCell_PointerTapped), true /*handledEventsToo*/);
  5. this.PointerCanceled += new PointerEventHandler(DataGridCell_PointerCanceled);
  6. this.PointerCaptureLost += new PointerEventHandler(DataGridCell_PointerCaptureLost);
  7. this.PointerPressed += new PointerEventHandler(DataGridCell_PointerPressed);
  8. this.PointerReleased += new PointerEventHandler(DataGridCell_PointerReleased);
  9. this.PointerEntered += new PointerEventHandler(DataGridCell_PointerEntered);
  10. this.PointerExited += new PointerEventHandler(DataGridCell_PointerExited);
  11. this.PointerMoved += new PointerEventHandler(DataGridCell_PointerMoved);
  12. DefaultStyleKey = typeof(DataGridCell);
  13. }

 

总结

这里我们把 DataGrid 的 DataGrid 相关类介绍完成了,代码部分的 CollectionView,Utilities 和 DataGrid 就介绍完了。因为代码本身比较复杂,量也很大,所以我们只挑选了一小部分代码来分享,大家具体用到时可以再具体分析。

接下来我们会就 DataGrid 控件的各种编辑功能,各种自定义功能等做进一步的使用方式的分享。

最后,再跟大家安利一下 WindowsCommunityToolkit 的官方微博:https://weibo.com/u/6506046490大家可以通过微博关注最新动态。

衷心感谢 WindowsCommunityToolkit 的作者们杰出的工作,感谢每一位贡献者,Thank you so much, ALL WindowsCommunityToolkit AUTHORS !!!

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号