Skip to content

Commit

Permalink
Merge pull request #17 from column-visibility
Browse files Browse the repository at this point in the history
added Visibility proeprty for columns
  • Loading branch information
w-ahmad committed Jun 3, 2024
2 parents 730ca9b + bfaf49c commit ebef34d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 20 deletions.
14 changes: 4 additions & 10 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public TableView()
{
DefaultStyleKey = typeof(TableView);

Columns = new();
CollectionView.Filter = Filter;
base.ItemsSource = CollectionView;

Expand Down Expand Up @@ -109,7 +108,7 @@ private string GetRowsContent(IEnumerable<object> items, bool includeHeaders, ch
foreach (var item in items)
{
var type = ItemsSource?.GetType() is { } listType && listType.IsGenericType ? listType.GetGenericArguments()[0] : item?.GetType();
foreach (var column in Columns.OfType<TableViewBoundColumn>())
foreach (var column in Columns.VisibleColumns.OfType<TableViewBoundColumn>())
{
var property = column.Binding.Path.Path;
if (!properties.TryGetValue(property, out var pis))
Expand All @@ -132,7 +131,7 @@ private string GetRowsContent(IEnumerable<object> items, bool includeHeaders, ch

private string GetHeadersContent(char separator)
{
return string.Join(separator, Columns.OfType<TableViewBoundColumn>().Select(x => x.Header));
return string.Join(separator, Columns.VisibleColumns.OfType<TableViewBoundColumn>().Select(x => x.Header));
}

internal async void SelectNextRow()
Expand Down Expand Up @@ -422,15 +421,11 @@ internal void ClearFilters()
}
}

public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();
public IAdvancedCollectionView CollectionView { get; } = new AdvancedCollectionView();

internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();

public TableViewColumnsCollection Columns
{
get => (TableViewColumnsCollection)GetValue(ColumnsProperty);
private set => SetValue(ColumnsProperty, value);
}
public TableViewColumnsCollection Columns { get; } = new();

public double HeaderRowHeight
{
Expand Down Expand Up @@ -499,7 +494,6 @@ public bool CanFilterColumns
}

public static readonly new DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(IList), typeof(TableView), new PropertyMetadata(null, OnItemsSourceChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(nameof(Columns), typeof(TableViewColumnsCollection), typeof(TableView), new PropertyMetadata(null));
public static readonly DependencyProperty HeaderRowHeightProperty = DependencyProperty.Register(nameof(HeaderRowHeight), typeof(double), typeof(TableView), new PropertyMetadata(32d, OnHeaderRowHeightChanged));
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(TableView), new PropertyMetadata(40d));
public static readonly DependencyProperty RowMaxHeightProperty = DependencyProperty.Register(nameof(RowMaxHeight), typeof(double), typeof(TableView), new PropertyMetadata(double.PositiveInfinity));
Expand Down
13 changes: 10 additions & 3 deletions src/WinUI.TableView/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ internal set
}
}

public Visibility Visibility
{
get => (Visibility)GetValue(VisibilityProperty);
set => SetValue(VisibilityProperty, value);
}

internal double DesiredWidth { get; set; }

public bool IsAutoGenerated { get; internal set; }

private void EnsureHeaderStyle()
Expand All @@ -72,13 +80,12 @@ private void EnsureHeaderStyle()
}
}

internal double DesiredWidth { get; set; }

public static readonly DependencyProperty HeaderStyleProperty = DependencyProperty.Register(nameof(HeaderStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, (d, _) => ((TableViewColumn)d).EnsureHeaderStyle()));
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(object), typeof(TableViewColumn), new PropertyMetadata(null));
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(nameof(Width), typeof(double), typeof(TableViewColumn), new PropertyMetadata(200d));
public static readonly DependencyProperty MinWidthProperty = DependencyProperty.Register(nameof(MinWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(50d));
public static readonly DependencyProperty MaxWidthProperty = DependencyProperty.Register(nameof(MaxWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(double.PositiveInfinity));
public static readonly DependencyProperty CanResizeProperty = DependencyProperty.Register(nameof(CanResize), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true));
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false));
}
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(nameof(Visibility), typeof(Visibility), typeof(TableViewColumn), new PropertyMetadata(Visibility.Visible));
}
61 changes: 59 additions & 2 deletions src/WinUI.TableView/TableViewColumnsColection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
using System.Collections.ObjectModel;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace WinUI.TableView;

public class TableViewColumnsCollection : ObservableCollection<TableViewColumn> { }
public class TableViewColumnsCollection : ObservableCollection<TableViewColumn>
{
private readonly Dictionary<TableViewColumn, long> _callbackMap = new();
internal event EventHandler<TableViewColumnVisibilityChanged>? ColumnVisibilityChanged;
internal IList<TableViewColumn> VisibleColumns => Items.Where(x => x.Visibility == Visibility.Visible).ToList();

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);

if (e.NewItems != null)
{
var index = e.NewStartingIndex;

foreach (var column in e.NewItems.OfType<TableViewColumn>())
{
var token = column.RegisterPropertyChangedCallback(TableViewColumn.VisibilityProperty, OnColumnVisibilityChanged);
_callbackMap.Remove(column);
_callbackMap.Add(column, token);
}
}

if (e.OldItems != null)
{
foreach (var column in e.OldItems.OfType<TableViewColumn>())
{
var token = _callbackMap[column];
column.UnregisterPropertyChangedCallback(TableViewColumn.VisibilityProperty, token);
}
}
}

private void OnColumnVisibilityChanged(DependencyObject sender, DependencyProperty dp)
{
if (sender is TableViewColumn column)
{
var index = IndexOf(column);
ColumnVisibilityChanged?.Invoke(this, new TableViewColumnVisibilityChanged(column, index));
}
}
}

internal class TableViewColumnVisibilityChanged : EventArgs
{
public TableViewColumnVisibilityChanged(TableViewColumn column, int index)
{
Column = column;
Index = index;
}

public TableViewColumn Column { get; }
public int Index { get; }
}
18 changes: 16 additions & 2 deletions src/WinUI.TableView/TableViewHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ async void ShowAndHidOptionsFlyout()
if (TableView.Columns is not null && GetTemplateChild("HeadersStackPanel") is StackPanel stackPanel)
{
_headersStackPanel = stackPanel;
AddHeaders(TableView.Columns);
AddHeaders(TableView.Columns.VisibleColumns);

TableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged;
TableView.Columns.ColumnVisibilityChanged += OnColumnVisibilityChanged;
}

SetExportOptionsVisibility();
Expand All @@ -86,7 +88,7 @@ private void OnTableViewColumnsCollectionChanged(object? sender, NotifyCollectio
{
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> newItems)
{
AddHeaders(newItems, e.NewStartingIndex);
AddHeaders(newItems.Where(x => x.Visibility == Visibility.Visible), e.NewStartingIndex);
}
else if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> oldItems)
{
Expand All @@ -98,6 +100,18 @@ private void OnTableViewColumnsCollectionChanged(object? sender, NotifyCollectio
}
}

private void OnColumnVisibilityChanged(object? sender, TableViewColumnVisibilityChanged e)
{
if (e.Column.Visibility == Visibility.Visible)
{
AddHeaders(new[] { e.Column }, e.Index);
}
else
{
RemoveHeaders(new[] { e.Column });
}
}

private void RemoveHeaders(IEnumerable<TableViewColumn> columns)
{
if (_headersStackPanel is not null)
Expand Down
20 changes: 17 additions & 3 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ protected override void OnApplyTemplate()

if (_tableView is not null)
{
AddCells(_tableView.Columns.VisibleColumns);

_tableView.Columns.CollectionChanged += OnColumnsCollectionChanged;
GenerateCells(_tableView.Columns);
_tableView.Columns.ColumnVisibilityChanged += OnColumnVisibilityChanged;
}
}

private void OnColumnsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> newItems)
{
GenerateCells(newItems, e.NewStartingIndex);
AddCells(newItems.Where(x => x.Visibility == Visibility.Visible), e.NewStartingIndex);
}
else if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems?.OfType<TableViewColumn>() is IEnumerable<TableViewColumn> oldItems)
{
Expand All @@ -49,6 +51,18 @@ private void OnColumnsCollectionChanged(object? sender, NotifyCollectionChangedE
}
}

private void OnColumnVisibilityChanged(object? sender, TableViewColumnVisibilityChanged e)
{
if (e.Column.Visibility == Visibility.Visible)
{
AddCells(new[] { e.Column }, e.Index);
}
else
{
RemoveCells(new[] { e.Column });
}
}

private void RemoveCells(IEnumerable<TableViewColumn> columns)
{
if (_cellsStackPanel is not null)
Expand All @@ -64,7 +78,7 @@ private void RemoveCells(IEnumerable<TableViewColumn> columns)
}
}

private void GenerateCells(IEnumerable<TableViewColumn> columns, int index = -1)
private void AddCells(IEnumerable<TableViewColumn> columns, int index = -1)
{
if (_cellsStackPanel is not null)
{
Expand Down

0 comments on commit ebef34d

Please sign in to comment.