diff --git a/src/WinUI.TableView/TableView.Properties.cs b/src/WinUI.TableView/TableView.Properties.cs index a4798df..69718be 100644 --- a/src/WinUI.TableView/TableView.Properties.cs +++ b/src/WinUI.TableView/TableView.Properties.cs @@ -55,9 +55,9 @@ public double RowMaxHeight set => SetValue(RowMaxHeightProperty, value); } - public new IList ItemsSource + public new IList? ItemsSource { - get => (IList)GetValue(ItemsSourceProperty); + get => (IList?)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } diff --git a/src/WinUI.TableView/TableView.cs b/src/WinUI.TableView/TableView.cs index a55e768..4863dcb 100644 --- a/src/WinUI.TableView/TableView.cs +++ b/src/WinUI.TableView/TableView.cs @@ -430,25 +430,24 @@ protected virtual void OnAutoGeneratingColumn(TableViewAutoGeneratingColumnEvent private static TableViewBoundColumn GetTableViewColumnFromType(Type type) { - switch (Type.GetTypeCode(type)) + return Type.GetTypeCode(type) switch { - case TypeCode.Byte: - case TypeCode.SByte: - case TypeCode.UInt16: - case TypeCode.UInt32: - case TypeCode.UInt64: - case TypeCode.Int16: - case TypeCode.Int32: - case TypeCode.Int64: - case TypeCode.Single: - case TypeCode.Double: - case TypeCode.Decimal: - return new TableViewNumberColumn(); - case TypeCode.Boolean: - return new TableViewCheckBoxColumn(); - default: - return new TableViewTextColumn(); - } + TypeCode.Byte + or TypeCode.SByte + or TypeCode.UInt16 + or TypeCode.UInt32 + or TypeCode.UInt64 + or TypeCode.Int16 + or TypeCode.Int32 + or TypeCode.Int64 + or TypeCode.Single + or TypeCode.Double + or TypeCode.Decimal + => new TableViewNumberColumn(), + TypeCode.Boolean + => new TableViewCheckBoxColumn(), + _ => new TableViewTextColumn(), + }; } private void OnItemsSourceChanged(DependencyPropertyChangedEventArgs e) diff --git a/src/WinUI.TableView/TableViewCell.cs b/src/WinUI.TableView/TableViewCell.cs index 236295e..78eeb7e 100644 --- a/src/WinUI.TableView/TableViewCell.cs +++ b/src/WinUI.TableView/TableViewCell.cs @@ -54,7 +54,7 @@ protected override Size MeasureOverride(Size availableSize) _contentPresenter ??= (ContentPresenter)GetTemplateChild("Content"); - var contentWidth = Column.ActualWidth; + var contentWidth = Column?.ActualWidth ?? 0d; contentWidth -= element.Margin.Left; contentWidth -= element.Margin.Right; contentWidth -= Padding.Left; @@ -97,8 +97,8 @@ protected override void OnPointerEntered(PointerRoutedEventArgs e) { base.OnPointerEntered(e); - if ((TableView.SelectionMode is not ListViewSelectionMode.None - && TableView.SelectionUnit is not TableViewSelectionUnit.Row) + if ((TableView?.SelectionMode is not ListViewSelectionMode.None + && TableView?.SelectionUnit is not TableViewSelectionUnit.Row) || !TableView.IsReadOnly) { VisualStates.GoToState(this, false, VisualStates.StatePointerOver); @@ -109,8 +109,8 @@ protected override void OnPointerExited(PointerRoutedEventArgs e) { base.OnPointerEntered(e); - if ((TableView.SelectionMode is not ListViewSelectionMode.None - && TableView.SelectionUnit is not TableViewSelectionUnit.Row) + if ((TableView?.SelectionMode is not ListViewSelectionMode.None + && TableView?.SelectionUnit is not TableViewSelectionUnit.Row) || !TableView.IsReadOnly) { VisualStates.GoToState(this, false, VisualStates.StateNormal); @@ -121,7 +121,7 @@ protected override void OnTapped(TappedRoutedEventArgs e) { base.OnTapped(e); - if (TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly) + if (TableView?.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly) { MakeSelection(); e.Handled = true; @@ -132,7 +132,7 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e) { base.OnPointerPressed(e); - if (!KeyBoardHelper.IsShiftKeyDown()) + if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null) { TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? Slot : default; ; TableView.SelectionStartRowIndex = Index; @@ -144,7 +144,7 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e) { base.OnPointerReleased(e); - if (!KeyBoardHelper.IsShiftKeyDown()) + if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null) { var cell = FindCell(e.GetCurrentPoint(this).Position); TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? cell?.Slot : default; @@ -167,14 +167,14 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) if (cell is not null && cell != this) { var ctrlKey = KeyBoardHelper.IsCtrlKeyDown(); - TableView.MakeSelection(cell.Slot, true, ctrlKey); + TableView?.MakeSelection(cell.Slot, true, ctrlKey); } } } private TableViewCell? FindCell(Point position) { - _scrollViewer ??= TableView.FindDescendant(); + _scrollViewer ??= TableView?.FindDescendant(); if (_scrollViewer is { }) { @@ -191,7 +191,7 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e) { - if (!IsReadOnly && !TableView.IsEditing && !Column.UseSingleElement) + if (!IsReadOnly && TableView is not null && !TableView.IsEditing && !Column?.UseSingleElement is true) { PrepareForEdit(); @@ -204,6 +204,11 @@ private void MakeSelection() var shiftKey = KeyBoardHelper.IsShiftKeyDown(); var ctrlKey = KeyBoardHelper.IsCtrlKeyDown(); + if (TableView is null || Column is null) + { + return; + } + if ((TableView.IsEditing || Column.UseSingleElement) && IsCurrent) { return; @@ -239,12 +244,12 @@ internal async void PrepareForEdit() internal void SetElement() { - Content = Column.GenerateElement(); + Content = Column?.GenerateElement(); } private void SetEditingElement() { - if (!Column.UseSingleElement) + if (Column?.UseSingleElement is false) { Content = Column.GenerateEditingElement(); } @@ -257,7 +262,7 @@ private void SetEditingElement() internal void RefreshElement() { - Column.RefreshElement(this, Content); + Column?.RefreshElement(this, Content); } internal void ApplySelectionState() @@ -284,10 +289,7 @@ internal void ApplyCurrentCellState() internal void UpdateElementState() { - if (Column is { }) - { - Column.UpdateElementState(this); - } + Column?.UpdateElementState(this); } private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -305,30 +307,30 @@ private static void OnColumnChanged(DependencyObject d, DependencyPropertyChange } } - public bool IsReadOnly => TableView.IsReadOnly || Column is TableViewTemplateColumn { EditingTemplate: null } or { IsReadOnly: true }; + public bool IsReadOnly => TableView?.IsReadOnly is true || Column is TableViewTemplateColumn { EditingTemplate: null } or { IsReadOnly: true }; - internal TableViewCellSlot Slot => new(Row.Index, Index); + internal TableViewCellSlot Slot => new(Row?.Index ?? -1, Index); internal int Index { get; set; } - public bool IsSelected => TableView.SelectedCells.Contains(Slot); - public bool IsCurrent => TableView.CurrentCellSlot == Slot; + public bool IsSelected => TableView?.SelectedCells.Contains(Slot) is true; + public bool IsCurrent => TableView?.CurrentCellSlot == Slot; - public TableViewColumn Column + public TableViewColumn? Column { - get => (TableViewColumn)GetValue(ColumnProperty); + get => (TableViewColumn?)GetValue(ColumnProperty); set => SetValue(ColumnProperty, value); } - public TableViewRow Row + public TableViewRow? Row { - get => (TableViewRow)GetValue(TableViewRowProperty); + get => (TableViewRow?)GetValue(TableViewRowProperty); set => SetValue(TableViewRowProperty, value); } - public TableView TableView + public TableView? TableView { - get => (TableView)GetValue(TableViewProperty); + get => (TableView?)GetValue(TableViewProperty); set => SetValue(TableViewProperty, value); } diff --git a/src/WinUI.TableView/TableViewColumn.cs b/src/WinUI.TableView/TableViewColumn.cs index b19d51b..aeed4b8 100644 --- a/src/WinUI.TableView/TableViewColumn.cs +++ b/src/WinUI.TableView/TableViewColumn.cs @@ -67,9 +67,9 @@ public bool IsReadOnly set => SetValue(IsReadOnlyProperty, value); } - public Style HeaderStyle + public Style? HeaderStyle { - get => (Style)GetValue(HeaderStyleProperty); + get => (Style?)GetValue(HeaderStyleProperty); set => SetValue(HeaderStyleProperty, value); } diff --git a/src/WinUI.TableView/TableViewComboBoxColumn.cs b/src/WinUI.TableView/TableViewComboBoxColumn.cs index 43238f4..b7db47d 100644 --- a/src/WinUI.TableView/TableViewComboBoxColumn.cs +++ b/src/WinUI.TableView/TableViewComboBoxColumn.cs @@ -42,21 +42,21 @@ public override FrameworkElement GenerateEditingElement() return comboBox; } - public object ItemsSource + public object? ItemsSource { get => GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } - public string DisplayMemberPath + public string? DisplayMemberPath { - get => (string)GetValue(DisplayMemberPathProperty); + get => (string?)GetValue(DisplayMemberPathProperty); set => SetValue(DisplayMemberPathProperty, value); } - public string SelectedValuePath + public string? SelectedValuePath { - get => (string)GetValue(SelectedValuePathProperty); + get => (string?)GetValue(SelectedValuePathProperty); set => SetValue(SelectedValuePathProperty, value); } diff --git a/src/WinUI.TableView/TableViewHeaderRow.cs b/src/WinUI.TableView/TableViewHeaderRow.cs index 6aeb977..2299da0 100644 --- a/src/WinUI.TableView/TableViewHeaderRow.cs +++ b/src/WinUI.TableView/TableViewHeaderRow.cs @@ -318,11 +318,11 @@ private void OnTableViewSelectionChanged() private void SetSelectAllButtonState() { - if (TableView.SelectionMode == ListViewSelectionMode.Multiple) + if (TableView?.SelectionMode == ListViewSelectionMode.Multiple) { VisualStates.GoToState(this, false, VisualStates.StateSelectAllCheckBox); } - else if (TableView.ShowOptionsButton) + else if (TableView?.ShowOptionsButton is true) { VisualStates.GoToState(this, false, VisualStates.StateOptionsButton); } @@ -376,9 +376,9 @@ private static void OnTableViewChanged(DependencyObject d, DependencyPropertyCha } } - public TableView TableView + public TableView? TableView { - get => (TableView)GetValue(TableViewProperty); + get => (TableView?)GetValue(TableViewProperty); set => SetValue(TableViewProperty, value); } diff --git a/src/WinUI.TableView/TableViewRow.cs b/src/WinUI.TableView/TableViewRow.cs index 2134581..3b023be 100644 --- a/src/WinUI.TableView/TableViewRow.cs +++ b/src/WinUI.TableView/TableViewRow.cs @@ -38,7 +38,7 @@ protected override void OnContentChanged(object oldContent, object newContent) protected override void OnPointerPressed(PointerRoutedEventArgs e) { - if (!KeyBoardHelper.IsShiftKeyDown()) + if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null) { TableView.SelectionStartRowIndex = Index; } @@ -46,7 +46,7 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e) protected override void OnPointerReleased(PointerRoutedEventArgs e) { - if (!KeyBoardHelper.IsShiftKeyDown()) + if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null) { TableView.SelectionStartCellSlot = null; TableView.SelectionStartRowIndex = null; @@ -58,6 +58,11 @@ protected override void OnTapped(TappedRoutedEventArgs e) var shiftKey = KeyBoardHelper.IsShiftKeyDown(); var ctrlKey = KeyBoardHelper.IsCtrlKeyDown(); + if (TableView is null) + { + return; + } + if (IsSelected && (ctrlKey || TableView.SelectionMode is ListViewSelectionMode.Multiple) && !shiftKey) { TableView.DeselectRange(new(Index, 1)); @@ -88,7 +93,7 @@ private void EnsureCells() private async void OnSizeChanged(object sender, SizeChangedEventArgs e) { - if (TableView.CurrentCellSlot?.Row == Index) + if (TableView?.CurrentCellSlot?.Row == Index) { _ = await TableView.ScrollCellIntoView(TableView.CurrentCellSlot.Value); } @@ -153,7 +158,7 @@ private void RemoveCells(IEnumerable columns) private void AddCells(IEnumerable columns, int index = -1) { - if (_cellPresenter is not null) + if (_cellPresenter is not null && TableView is not null) { foreach (var column in columns) { @@ -242,11 +247,11 @@ internal void ApplyCellsSelectionState() internal IList Cells => _cellPresenter?.Cells ?? new List(); - public int Index => TableView.IndexFromContainer(this); + public int Index => TableView?.IndexFromContainer(this) ?? -1; - public TableView TableView + public TableView? TableView { - get => (TableView)GetValue(TableViewProperty); + get => (TableView?)GetValue(TableViewProperty); set => SetValue(TableViewProperty, value); } diff --git a/src/WinUI.TableView/TableViewTemplateColumn.cs b/src/WinUI.TableView/TableViewTemplateColumn.cs index 3c98016..d47a96a 100644 --- a/src/WinUI.TableView/TableViewTemplateColumn.cs +++ b/src/WinUI.TableView/TableViewTemplateColumn.cs @@ -32,27 +32,27 @@ public override FrameworkElement GenerateEditingElement() return GenerateElement(); } - public DataTemplate CellTemplate + public DataTemplate? CellTemplate { - get => (DataTemplate)GetValue(CellTemplateProperty); + get => (DataTemplate?)GetValue(CellTemplateProperty); set => SetValue(CellTemplateProperty, value); } - public DataTemplateSelector CellTemplateSelector + public DataTemplateSelector? CellTemplateSelector { - get => (DataTemplateSelector)GetValue(CellTemplateSelectorProperty); + get => (DataTemplateSelector?)GetValue(CellTemplateSelectorProperty); set => SetValue(CellTemplateSelectorProperty, value); } - public DataTemplate EditingTemplate + public DataTemplate? EditingTemplate { - get => (DataTemplate)GetValue(EditingTemplateProperty); + get => (DataTemplate?)GetValue(EditingTemplateProperty); set => SetValue(EditingTemplateProperty, value); } - public DataTemplateSelector EditingTemplateSelector + public DataTemplateSelector? EditingTemplateSelector { - get => (DataTemplateSelector)GetValue(EditingTemplateSelectorProperty); + get => (DataTemplateSelector?)GetValue(EditingTemplateSelectorProperty); set => SetValue(EditingTemplateSelectorProperty, value); } diff --git a/src/WinUI.TableView/TableViewToggleSwitchColumn.cs b/src/WinUI.TableView/TableViewToggleSwitchColumn.cs index 973ed3b..d5d8f43 100644 --- a/src/WinUI.TableView/TableViewToggleSwitchColumn.cs +++ b/src/WinUI.TableView/TableViewToggleSwitchColumn.cs @@ -45,12 +45,12 @@ private void UpdateToggleButtonState(ToggleSwitch toggleSwitch) toggleSwitch.IsHitTestVisible = TableView?.IsReadOnly is false && !IsReadOnly; } - public object OnContent + public object? OnContent { get => GetValue(OnContentProperty); set => SetValue(OnContentProperty, value); } - public object OffContent + public object? OffContent { get => GetValue(OffContentProperty); set => SetValue(OffContentProperty, value);