From 2f741fe86631da0a57bfee80b27feaef1cb85c71 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Wed, 18 Jan 2023 17:50:38 +0100 Subject: [PATCH 1/4] feat: Allow change visibility of column --- samples/TreeDataGridDemo/MainWindow.axaml | 12 +++++++++++ .../ViewModels/CountriesPageViewModel.cs | 1 + .../Models/TreeDataGrid/ColumnBase`1.cs | 20 +++++++++++++++++-- .../Models/TreeDataGrid/ColumnList.cs | 4 ++++ .../Models/TreeDataGrid/ColumnOptions.cs | 5 +++++ .../HierarchicalExpanderColumn.cs | 7 +++++++ .../Models/TreeDataGrid/IColumn.cs | 5 +++++ .../Models/TreeDataGrid/IColumns.cs | 5 +++++ .../Primitives/TreeDataGridColumnHeader.cs | 12 ++++++++++- .../TreeDataGridColumnHeadersPresenter.cs | 9 ++++++--- 10 files changed, 74 insertions(+), 6 deletions(-) diff --git a/samples/TreeDataGridDemo/MainWindow.axaml b/samples/TreeDataGridDemo/MainWindow.axaml index 3a69b043..4d39ef86 100644 --- a/samples/TreeDataGridDemo/MainWindow.axaml +++ b/samples/TreeDataGridDemo/MainWindow.axaml @@ -25,6 +25,18 @@ + + + + + + + + + + diff --git a/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs b/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs index 8432c20a..c15ccb3e 100644 --- a/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs +++ b/samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs @@ -28,6 +28,7 @@ public CountriesPageViewModel() new TextColumn("Area", x => x.Area, new GridLength(3, GridUnitType.Star)), new TextColumn("GDP", x => x.GDP, new GridLength(3, GridUnitType.Star), new() { + IsVisible = true, MaxWidth = new GridLength(150) }), } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnBase`1.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnBase`1.cs index 87156971..d7c377db 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnBase`1.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnBase`1.cs @@ -20,6 +20,7 @@ public abstract class ColumnBase : NotifyingBase, IColumn, IUpda private bool _starWidthWasConstrained; private object? _header; private ListSortDirection? _sortDirection; + private bool _isVisible = true; /// /// Initializes a new instance of the class. @@ -38,6 +39,7 @@ public ColumnBase( _minWidth = options?.MinWidth ?? new GridLength(30, GridUnitType.Pixel); _maxWidth = options?.MaxWidth; _header = header; + _isVisible = options?.IsVisible ?? true; SetWidth(width ?? GridLength.Auto); } @@ -65,7 +67,7 @@ public bool? CanUserResize /// /// To set the column width use . /// - public GridLength Width + public GridLength Width { get => _width; private set => RaiseAndSetIfChanged(ref _width, value); @@ -100,7 +102,10 @@ public ListSortDirection? SortDirection public object? Tag { get; set; } double IUpdateColumnLayout.MinActualWidth => CoerceActualWidth(0); - double IUpdateColumnLayout.MaxActualWidth => CoerceActualWidth(double.PositiveInfinity); + double IUpdateColumnLayout.MaxActualWidth => + IsVisible + ? CoerceActualWidth(double.PositiveInfinity) + : 0; bool IUpdateColumnLayout.StarWidthWasConstrained => _starWidthWasConstrained; /// @@ -114,6 +119,10 @@ public ListSortDirection? SortDirection double IUpdateColumnLayout.CellMeasured(double width, int rowIndex) { + if (!IsVisible) + { + return 0; + } _autoWidth = Math.Max(NonNaN(_autoWidth), CoerceActualWidth(width)); return Width.GridUnitType == GridUnitType.Auto || double.IsNaN(ActualWidth) ? _autoWidth : ActualWidth; @@ -175,5 +184,12 @@ private void SetWidth(GridLength width) } private static double NonNaN(double v) => double.IsNaN(v) ? 0 : v; + + public bool IsVisible + { + get => _isVisible; + set => RaiseAndSetIfChanged(ref _isVisible, value); + } + } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnList.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnList.cs index ae10499d..3b72e90b 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnList.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnList.cs @@ -193,5 +193,9 @@ private void UpdateColumnSizes() } private static double NotNaN(double v) => double.IsNaN(v) ? 0 : v; + + void IColumns.InvalidateLayout() => + LayoutInvalidated?.Invoke(this,EventArgs.Empty); + } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs index 1bfc5de3..ee76a9cf 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs @@ -45,5 +45,10 @@ public class ColumnOptions /// Gets or sets a custom comparison for descending ordered columns. /// public Comparison? CompareDescending { get; set; } + + /// + /// Determines whether or not this column is visible. + /// + public bool IsVisible { get; set; } } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/HierarchicalExpanderColumn.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/HierarchicalExpanderColumn.cs index 0755ca0a..9d17a440 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/HierarchicalExpanderColumn.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/HierarchicalExpanderColumn.cs @@ -23,6 +23,7 @@ public class HierarchicalExpanderColumn : NotifyingBase, private readonly TypedBinding? _hasChildrenSelector; private readonly TypedBinding? _isExpandedBinding; private double _actualWidth = double.NaN; + private bool _isVisible = true; /// /// Initializes a new instance of the class. @@ -160,5 +161,11 @@ private void SetWidth(GridLength width) if (width.IsAbsolute) ActualWidth = width.Value; } + + public bool IsVisible + { + get => _isVisible; + set => RaiseAndSetIfChanged(ref _isVisible, value); + } } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumn.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumn.cs index 21ba7496..9a5b431c 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumn.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumn.cs @@ -47,5 +47,10 @@ public interface IColumn : INotifyPropertyChanged /// Gets or sets a user-defined object attached to the column. /// object? Tag { get; set; } + + /// + /// Determines whether or not this column is visible. + /// + bool IsVisible { get; set; } } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumns.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumns.cs index b54d2bd0..c6182e15 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumns.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/IColumns.cs @@ -65,5 +65,10 @@ public interface IColumns : IReadOnlyList, INotifyCollectionChanged /// /// The current viewport. void ViewportChanged(Rect viewport); + + /// + /// Signal that the columns layout has changed. + /// + void InvalidateLayout(); } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs index 45dd8855..23e77ebb 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeader.cs @@ -30,6 +30,7 @@ public class TreeDataGridColumnHeader : Button private ListSortDirection? _sortDirection; private TreeDataGrid? _owner; private Thumb? _resizer; + private static readonly GridLength Zero = new GridLength(0, GridUnitType.Pixel); public bool CanUserResize { @@ -141,7 +142,9 @@ private void OnModelPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(IColumn.CanUserResize) || e.PropertyName == nameof(IColumn.Header) || - e.PropertyName == nameof(IColumn.SortDirection)) + e.PropertyName == nameof(IColumn.SortDirection) + || e.PropertyName == nameof(IColumn.IsVisible) + ) UpdatePropertiesFromModel(); } @@ -169,9 +172,16 @@ private void ResizerDragDelta(object? sender, VectorEventArgs e) private void UpdatePropertiesFromModel() { + var oldVisibility = IsVisible; CanUserResize = _model?.CanUserResize ?? _owner?.CanUserResizeColumns ?? false; Header = _model?.Header; SortDirection = _model?.SortDirection; + IsVisible = _model?.IsVisible == true; + if(IsVisible!= oldVisibility) + { + _columns?.InvalidateLayout(); + } + } } } diff --git a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeadersPresenter.cs b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeadersPresenter.cs index 196dea0b..71bcb0cc 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeadersPresenter.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Primitives/TreeDataGridColumnHeadersPresenter.cs @@ -20,9 +20,12 @@ protected override Size ArrangeOverride(Size finalSize) protected override Size MeasureElement(int index, IControl element, Size availableSize) { - var columns = (IColumns)Items!; - element.Measure(availableSize); - return columns.CellMeasured(index, -1, element.DesiredSize); + if (Items is IColumns columns) + { + element.Measure(availableSize); + return columns.CellMeasured(index, -1, element.DesiredSize); + } + return Size.Empty; } protected override void RealizeElement(IControl element, IColumn column, int index) From cc8c8e20ad0876ae798ba83a7da260bc79839903 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 19 Jan 2023 12:50:49 +0100 Subject: [PATCH 2/4] fix: Sample --- samples/TreeDataGridDemo/MainWindow.axaml | 27 +++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/samples/TreeDataGridDemo/MainWindow.axaml b/samples/TreeDataGridDemo/MainWindow.axaml index 4d39ef86..ac448c28 100644 --- a/samples/TreeDataGridDemo/MainWindow.axaml +++ b/samples/TreeDataGridDemo/MainWindow.axaml @@ -25,18 +25,21 @@ - - - - - - - - - - + + + + + + + + + + + + + From 7784b159b96df3eab34287c5f3eae90f8bf339e0 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Thu, 19 Jan 2023 14:34:28 +0100 Subject: [PATCH 3/4] fix: ColumnOptions IsVisible default vaule --- .../Models/TreeDataGrid/ColumnOptions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs index ee76a9cf..b31642ec 100644 --- a/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs +++ b/src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/ColumnOptions.cs @@ -49,6 +49,6 @@ public class ColumnOptions /// /// Determines whether or not this column is visible. /// - public bool IsVisible { get; set; } + public bool IsVisible { get; set; } = true; } } From f0de7bd1b5d47708762ab36f4eca6fd99a797f57 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 16 May 2023 16:53:06 +0200 Subject: [PATCH 4/4] fix: Sample --- samples/TreeDataGridDemo/MainWindow.axaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/TreeDataGridDemo/MainWindow.axaml b/samples/TreeDataGridDemo/MainWindow.axaml index fe1e8467..47821ef3 100644 --- a/samples/TreeDataGridDemo/MainWindow.axaml +++ b/samples/TreeDataGridDemo/MainWindow.axaml @@ -30,7 +30,7 @@ DockPanel.Dock="Top"> - +