概述html
在上面一篇 Windows Community Toolkit 4.0 - DataGrid - Part02 中,咱們針對 DataGrid 控件的 Utilities 部分作了詳細分享。而在本篇,咱們會對控件中最重要的 DataGrid 文件夾中的類作詳細的分享。git
下面是 Windows Community Toolkit Sample App 的示例截圖和 code/doc 地址:github
Windows Community Toolkit Doc - DataGridwindows
Windows Community Toolkit Source Code - DataGridapp
Namespace: Microsoft.Toolkit.Uwp.UI.Controls; Nuget: Microsoft.Toolkit.Uwp.UI.Controls.DataGrid;less
開發過程dom
DataGrid 文件夾中是 DataGrid 控件最重要的功能,首先咱們仍是先來看一下類結構:ide
包括了 Automation;DataGrid,DataGridColumn,DataGridRow,DataGridCell 控件實現,事件處理參數類和數據類等;this
接着咱們看幾個重要的類和方法:spa
1. DataGrid.cs
這個類是 DataGrid 控件的主要處理類,功能也是比較複雜,單個類的代碼行數是 9001 行,咱們只挑兩個方法來看一下。其餘方法你們有興趣或用到時能夠在 DataGrid.cs 中查閱。
1) DataGrid()
首先看一下 DataGrid 類的構造方法,之因此看這個方法,是想讓你們能夠更瞭解 DataGrid 類中變量的初始化方式,這些變量在不一樣的交互場景下會被賦予不一樣的值。
public DataGrid() { this.TabNavigation = KeyboardNavigationMode.Once; _loadedRows = new List<DataGridRow>(); _lostFocusActions = new Queue<Action>(); _selectedItems = new DataGridSelectedItemsCollection(this); _rowGroupHeaderPropertyNameAlternative = Properties.Resources.DefaultRowGroupHeaderPropertyNameAlternative; _rowGroupHeaderStyles = new ObservableCollection<Style>(); _rowGroupHeaderStyles.CollectionChanged += RowGroupHeaderStyles_CollectionChanged; _rowGroupHeaderStylesOld = new List<Style>(); this.RowGroupHeadersTable = new IndexToValueTable<DataGridRowGroupInfo>(); _collapsedSlotsTable = new IndexToValueTable<Visibility>(); _validationItems = new Dictionary<INotifyDataErrorInfo, string>(); _validationResults = new List<ValidationResult>(); _bindingValidationResults = new List<ValidationResult>(); _propertyValidationResults = new List<ValidationResult>(); _indeiValidationResults = new List<ValidationResult>(); this.ColumnHeaderInteractionInfo = new DataGridColumnHeaderInteractionInfo(); this.DisplayData = new DataGridDisplayData(this); this.ColumnsInternal = CreateColumnsInstance(); this.RowHeightEstimate = DATAGRID_defaultRowHeight; this.RowDetailsHeightEstimate = 0; _rowHeaderDesiredWidth = 0; this.DataConnection = new DataGridDataConnection(this); _showDetailsTable = new IndexToValueTable<Visibility>(); _focusInputDevice = FocusInputDeviceKind.None; _proposedScrollBarsState = ScrollBarVisualState.NoIndicator; _proposedScrollBarsSeparatorState = ScrollBarsSeparatorVisualState.SeparatorCollapsed; this.AnchorSlot = -1; _lastEstimatedRow = -1; _editingColumnIndex = -1; this.CurrentCellCoordinates = new DataGridCellCoordinates(-1, -1); this.RowGroupHeaderHeightEstimate = DATAGRID_defaultRowHeight; this.LastHandledKeyDown = VirtualKey.None; this.DefaultStyleKey = typeof(DataGrid); HookDataGridEvents(); }
2) ShowScrollBars()
DataGrid 控件中滾動條的處理方法。若是 AreAllScrollBarsCollapsed 爲 true,則按照該規則簡單處理;若是爲 false,先按照 mouse 和 touch 的類型進行判斷處理,再根據 UI 設置裏的 AreSettingsEnablingAnimations 和 AreSettingsAutoHidingScrollBars 屬性來切換滾動條的狀態,調用 SwitchScrollBarsVisualStates 方法。
private void ShowScrollBars() { if (this.AreAllScrollBarsCollapsed) { _proposedScrollBarsState = ScrollBarVisualState.NoIndicator; _proposedScrollBarsSeparatorState = ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation; SwitchScrollBarsVisualStates(_proposedScrollBarsState, _proposedScrollBarsSeparatorState, false /*useTransitions*/); } else { if (_hideScrollBarsTimer != null && _hideScrollBarsTimer.IsEnabled) { _hideScrollBarsTimer.Stop(); _hideScrollBarsTimer.Start(); } // Mouse indicators dominate if they are already showing or if we have set the flag to prefer them. if (_preferMouseIndicators || _showingMouseIndicators) { if (this.AreBothScrollBarsVisible && (_isPointerOverHorizontalScrollBar || _isPointerOverVerticalScrollBar)) { _proposedScrollBarsState = ScrollBarVisualState.MouseIndicatorFull; } else { _proposedScrollBarsState = ScrollBarVisualState.MouseIndicator; } _showingMouseIndicators = true; } else { _proposedScrollBarsState = ScrollBarVisualState.TouchIndicator; } // Select the proper state for the scroll bars separator square within the GroupScrollBarsSeparator group: if (UISettingsHelper.AreSettingsEnablingAnimations) { // When OS animations are turned on, show the square when a scroll bar is shown unless the DataGrid is disabled, using an animation. _proposedScrollBarsSeparatorState = this.IsEnabled && _proposedScrollBarsState == ScrollBarVisualState.MouseIndicatorFull ? ScrollBarsSeparatorVisualState.SeparatorExpanded : ScrollBarsSeparatorVisualState.SeparatorCollapsed; } else { // OS animations are turned off. Show or hide the square depending on the presence of a scroll bars, without an animation. // When the DataGrid is disabled, hide the square in sync with the scroll bar(s). if (_proposedScrollBarsState == ScrollBarVisualState.MouseIndicatorFull) { _proposedScrollBarsSeparatorState = this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpandedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed; } else { _proposedScrollBarsSeparatorState = this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed; } } if (!UISettingsHelper.AreSettingsAutoHidingScrollBars) { if (this.AreBothScrollBarsVisible) { if (UISettingsHelper.AreSettingsEnablingAnimations) { SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicatorFull, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpanded : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/); } else { SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicatorFull, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorExpandedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/); } } else { if (UISettingsHelper.AreSettingsEnablingAnimations) { SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicator, ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/); } else { SwitchScrollBarsVisualStates(ScrollBarVisualState.MouseIndicator, this.IsEnabled ? ScrollBarsSeparatorVisualState.SeparatorCollapsedWithoutAnimation : ScrollBarsSeparatorVisualState.SeparatorCollapsed, true /*useTransitions*/); } } } else { SwitchScrollBarsVisualStates(_proposedScrollBarsState, _proposedScrollBarsSeparatorState, true /*useTransitions*/); } } }
2. DataGridCellEditEndedEventArgs.cs
DataGrid 控件中有不少事件處理參數類,咱們只看其中一個 DataGridCellEditEndedEventArgs 吧。很顯然這個事件包含了 column row 和 editAction 三個變量,你們看到其餘時間參數時,能夠具體再分析。
public class DataGridCellEditEndedEventArgs : EventArgs { public DataGridCellEditEndedEventArgs(DataGridColumn column, DataGridRow row, DataGridEditAction editAction) { this.Column = column; this.Row = row; this.EditAction = editAction; } public DataGridColumn Column { get; private set; } public DataGridEditAction EditAction { get; private set; } public DataGridRow Row { get; private set; } }
3. DataGridCellCollection.cs
DataGrid 控件中有不少數據類,咱們看一個單元格集合類,能夠看到集合中有 _cells,Count 變量,Insert 和 RemoveAt 方法等,處理邏輯都比較簡單。
internal class DataGridCellCollection { private List<DataGridCell> _cells; private DataGridRow _owningRow; internal event EventHandler<DataGridCellEventArgs> CellAdded; internal event EventHandler<DataGridCellEventArgs> CellRemoved; public DataGridCellCollection(DataGridRow owningRow) { _owningRow = owningRow; _cells = new List<DataGridCell>(); } public int Count { get { return _cells.Count; } } public IEnumerator GetEnumerator() { return _cells.GetEnumerator(); } public void Insert(int cellIndex, DataGridCell cell) { Debug.Assert(cellIndex >= 0 && cellIndex <= _cells.Count, "Expected cellIndex between 0 and _cells.Count inclusive."); Debug.Assert(cell != null, "Expected non-null cell."); cell.OwningRow = _owningRow; _cells.Insert(cellIndex, cell); if (CellAdded != null) { CellAdded(this, new DataGridCellEventArgs(cell)); } } public void RemoveAt(int cellIndex) { DataGridCell dataGridCell = _cells[cellIndex]; _cells.RemoveAt(cellIndex); dataGridCell.OwningRow = null; if (CellRemoved != null) { CellRemoved(this, new DataGridCellEventArgs(dataGridCell)); } } public DataGridCell this[int index] { get { if (index < 0 || index >= _cells.Count) { throw DataGridError.DataGrid.ValueMustBeBetween("index", "Index", 0, true, _cells.Count, false); } return _cells[index]; } } }
4. DataGridCell.cs
DataGrid 控件的單元格類,處理比較簡單,咱們經過構造方法來看一下類中都涉及到哪些事件的處理;能夠看到,光標的一系列處理都有涉及。
public DataGridCell() { this.IsTapEnabled = true; this.AddHandler(UIElement.TappedEvent, new TappedEventHandler(DataGridCell_PointerTapped), true /*handledEventsToo*/); this.PointerCanceled += new PointerEventHandler(DataGridCell_PointerCanceled); this.PointerCaptureLost += new PointerEventHandler(DataGridCell_PointerCaptureLost); this.PointerPressed += new PointerEventHandler(DataGridCell_PointerPressed); this.PointerReleased += new PointerEventHandler(DataGridCell_PointerReleased); this.PointerEntered += new PointerEventHandler(DataGridCell_PointerEntered); this.PointerExited += new PointerEventHandler(DataGridCell_PointerExited); this.PointerMoved += new PointerEventHandler(DataGridCell_PointerMoved); DefaultStyleKey = typeof(DataGridCell); }
總結
這裏咱們把 DataGrid 的 DataGrid 相關類介紹完成了,代碼部分的 CollectionView,Utilities 和 DataGrid 就介紹完了。由於代碼自己比較複雜,量也很大,因此咱們只挑選了一小部分代碼來分享,你們具體用到時能夠再具體分析。
接下來咱們會就 DataGrid 控件的各類編輯功能,各類自定義功能等作進一步的使用方式的分享。
最後,再跟你們安利一下 WindowsCommunityToolkit 的官方微博:https://weibo.com/u/6506046490, 你們能夠經過微博關注最新動態。
衷心感謝 WindowsCommunityToolkit 的做者們傑出的工做,感謝每一位貢獻者,Thank you so much, ALL WindowsCommunityToolkit AUTHORS !!!