Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow change visibility of column #148

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2f741fe
feat: Allow change visibility of column
workgroupengineering Jan 18, 2023
cc8c8e2
fix: Sample
workgroupengineering Jan 19, 2023
7784b15
fix: ColumnOptions IsVisible default vaule
workgroupengineering Jan 19, 2023
e7cd6c1
fix: Alignment to Avalonia 11 Preview 6
workgroupengineering Mar 30, 2023
125a381
fix: Conflict with matser
workgroupengineering Apr 14, 2023
e3e847d
Merge branch 'master' into feature/Column_Visibility
workgroupengineering May 16, 2023
f0de7bd
fix: Sample
workgroupengineering May 16, 2023
7417877
fix: Conflicts
workgroupengineering Jun 15, 2023
c84d94d
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Jul 6, 2023
212a99d
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Aug 28, 2023
4303413
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Sep 6, 2023
9ab8607
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Sep 30, 2023
31bc5d5
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Oct 19, 2023
910476a
fix: Conflict with master
workgroupengineering Nov 2, 2023
9ea994a
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Dec 11, 2023
b8ed3be
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Dec 27, 2023
09509db
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Feb 19, 2024
436e58a
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Apr 3, 2024
c562136
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Apr 18, 2024
5570e26
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Jul 9, 2024
c61ffa4
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Aug 21, 2024
ecfcc78
Merge branch 'master' into feature/Column_Visibility
workgroupengineering Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions samples/TreeDataGridDemo/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@
<Button Click="AddCountryClick">Add</Button>
<Button Command="{Binding Countries.RemoveSelected}">Remove</Button>
</StackPanel>
<DropDownButton Content="Columns Visibility"
DockPanel.Dock="Top">
<DropDownButton.Flyout>
<Flyout Placement="Bottom">
<ItemsControl ItemsSource="{Binding #countries.Columns}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsVisible}"
Content="{Binding Header,FallbackValue=none}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Flyout>
</DropDownButton.Flyout>
</DropDownButton>
<TreeDataGrid Name="countries"
Source="{Binding Countries.Source}"
AutoDragDropRows="True">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public CountriesPageViewModel()
new TextColumn<Country, int>("GDP", x => x.GDP, new GridLength(3, GridUnitType.Star), new()
{
TextAlignment = Avalonia.Media.TextAlignment.Right,
IsVisible = true,
MaxWidth = new GridLength(150)
}),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public double ActualWidth
/// <remarks>
/// To set the column width use <see cref="IColumns.SetColumnWidth(int, GridLength)"/>.
/// </remarks>
public GridLength Width
public GridLength Width
{
get => _width;
private set => RaiseAndSetIfChanged(ref _width, value);
Expand Down Expand Up @@ -92,7 +92,10 @@ public ListSortDirection? SortDirection

bool? IColumn.CanUserResize => Options.CanUserResizeColumn;
double IUpdateColumnLayout.MinActualWidth => CoerceActualWidth(0);
double IUpdateColumnLayout.MaxActualWidth => CoerceActualWidth(double.PositiveInfinity);
double IUpdateColumnLayout.MaxActualWidth =>
IsVisible
? CoerceActualWidth(double.PositiveInfinity)
: 0;
bool IUpdateColumnLayout.StarWidthWasConstrained => _starWidthWasConstrained;

/// <summary>
Expand All @@ -106,6 +109,10 @@ public ListSortDirection? SortDirection

double IUpdateColumnLayout.CellMeasured(double width, int rowIndex)
{
if (!Options.IsVisible)
{
return 0;
}
_autoWidth = Math.Max(NonNaN(_autoWidth), CoerceActualWidth(width));
return Width.GridUnitType == GridUnitType.Auto || double.IsNaN(ActualWidth) ?
_autoWidth : ActualWidth;
Expand Down Expand Up @@ -167,5 +174,19 @@ private void SetWidth(GridLength width)
}

private static double NonNaN(double v) => double.IsNaN(v) ? 0 : v;

public bool IsVisible
{
get => Options.IsVisible;
set
{
if (Options.IsVisible != value)
{
Options.IsVisible = value;
RaisePropertyChanged(nameof(IsVisible));
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,9 @@ private void UpdateColumnSizes()
}

private static double NotNaN(double v) => double.IsNaN(v) ? 0 : v;

void IColumns.InvalidateLayout() =>
LayoutInvalidated?.Invoke(this,EventArgs.Empty);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@ public class ColumnOptions<TModel>
/// Gets or sets the gesture(s) that will cause a cell to enter edit mode.
/// </summary>
public BeginEditGestures BeginEditGestures { get; set; } = BeginEditGestures.Default;

/// Determines whether or not this column is visible.
/// </summary>
public bool IsVisible { get; set; } = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class HierarchicalExpanderColumn<TModel> : NotifyingBase,
private readonly TypedBinding<TModel, bool>? _hasChildrenSelector;
private readonly TypedBinding<TModel, bool>? _isExpandedBinding;
private double _actualWidth = double.NaN;
private bool _isVisible = true;

/// <summary>
/// Initializes a new instance of the <see cref="HierarchicalExpanderColumn{TModel}"/> class.
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ public interface IColumn : INotifyPropertyChanged
/// Gets or sets a user-defined object attached to the column.
/// </summary>
object? Tag { get; set; }

/// <summary>
/// Determines whether or not this column is visible.
/// </summary>
bool IsVisible { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,10 @@ public interface IColumns : IReadOnlyList<IColumn>, INotifyCollectionChanged
/// </summary>
/// <param name="viewport">The current viewport.</param>
void ViewportChanged(Rect viewport);

/// <summary>
/// Signal that the columns layout has changed.
/// </summary>
void InvalidateLayout();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -146,7 +147,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();
}

Expand Down Expand Up @@ -174,9 +177,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();
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ protected override Size ArrangeOverride(Size finalSize)

protected override Size MeasureElement(int index, Control 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 default;
}

protected override void RealizeElement(Control element, IColumn column, int index)
Expand Down
Loading