Skip to content

Commit

Permalink
convert Row and Cell dependency properties to standard properties
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Nov 21, 2024
1 parent aa621b9 commit dfafee8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
44 changes: 19 additions & 25 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace WinUI.TableView;
[TemplateVisualState(Name = VisualStates.StateUnselected, GroupName = VisualStates.GroupSelection)]
public class TableViewCell : ContentControl
{
private TableViewColumn? _column;
private ScrollViewer? _scrollViewer;
private ContentPresenter? _contentPresenter;

Expand Down Expand Up @@ -289,18 +290,15 @@ internal void UpdateElementState()
Column?.UpdateElementState(this);
}

private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private void OnColumnChanged()
{
if (d is TableViewCell cell)
if (TableView?.IsEditing == true)
{
if (cell.TableView?.IsEditing == true)
{
cell.SetEditingElement();
}
else
{
cell.SetElement();
}
SetEditingElement();
}
else
{
SetElement();
}
}

Expand All @@ -315,23 +313,19 @@ private static void OnColumnChanged(DependencyObject d, DependencyPropertyChange

public TableViewColumn? Column
{
get => (TableViewColumn?)GetValue(ColumnProperty);
set => SetValue(ColumnProperty, value);
get => _column;
internal set
{
if (_column != value)
{
_column = value;
OnColumnChanged();
}
}
}

public TableViewRow? Row
{
get => (TableViewRow?)GetValue(TableViewRowProperty);
set => SetValue(TableViewRowProperty, value);
}
public TableViewRow? Row { get; internal set; }

public TableView? TableView
{
get => (TableView?)GetValue(TableViewProperty);
set => SetValue(TableViewProperty, value);
}
public TableView? TableView { get; internal set; }

public static readonly DependencyProperty ColumnProperty = DependencyProperty.Register(nameof(Column), typeof(TableViewColumn), typeof(TableViewCell), new PropertyMetadata(default, OnColumnChanged));
public static readonly DependencyProperty TableViewRowProperty = DependencyProperty.Register(nameof(Row), typeof(TableViewRow), typeof(TableViewCell), new PropertyMetadata(default));
public static readonly DependencyProperty TableViewProperty = DependencyProperty.Register(nameof(TableView), typeof(TableView), typeof(TableViewCell), new PropertyMetadata(default));
}
45 changes: 25 additions & 20 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace WinUI.TableView;

public class TableViewRow : ListViewItem
{
private TableView? _tableView;
private TableViewCellsPresenter? _cellPresenter;
private bool _ensureCells = true;

Expand Down Expand Up @@ -168,32 +169,30 @@ private void AddCells(IEnumerable<TableViewColumn> columns, int index = -1)
}
}

private static void OnTableViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private void OnTableViewChanging()
{
if (d is not TableViewRow row)
if (TableView is not null)
{
return;
}

if (e.NewValue is TableView newTableView)
{
newTableView.IsReadOnlyChanged += row.OnTableViewIsReadOnlyChanged;
TableView.IsReadOnlyChanged -= OnTableViewIsReadOnlyChanged;

if (newTableView.Columns is not null)
if (TableView.Columns is not null)
{
newTableView.Columns.CollectionChanged += row.OnColumnsCollectionChanged;
newTableView.Columns.ColumnPropertyChanged += row.OnColumnPropertyChanged;
TableView.Columns.CollectionChanged -= OnColumnsCollectionChanged;
TableView.Columns.ColumnPropertyChanged -= OnColumnPropertyChanged;
}
}
}

if (e.OldValue is TableView oldTableView && oldTableView.Columns is not null)
private void OnTableViewChanged()
{
if (TableView is not null)
{
oldTableView.IsReadOnlyChanged -= row.OnTableViewIsReadOnlyChanged;
TableView.IsReadOnlyChanged += OnTableViewIsReadOnlyChanged;

if (oldTableView.Columns is not null)
if (TableView.Columns is not null)
{
oldTableView.Columns.CollectionChanged -= row.OnColumnsCollectionChanged;
oldTableView.Columns.ColumnPropertyChanged -= row.OnColumnPropertyChanged;
TableView.Columns.CollectionChanged += OnColumnsCollectionChanged;
TableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged;
}
}
}
Expand Down Expand Up @@ -234,9 +233,15 @@ internal void ApplyCellsSelectionState()

public TableView? TableView
{
get => (TableView?)GetValue(TableViewProperty);
set => SetValue(TableViewProperty, value);
get => _tableView;
internal set
{
if (_tableView != value)
{
OnTableViewChanging();
_tableView = value;
OnTableViewChanged();
}
}
}

public static readonly DependencyProperty TableViewProperty = DependencyProperty.Register(nameof(TableView), typeof(TableView), typeof(TableViewRow), new PropertyMetadata(default, OnTableViewChanged));
}

0 comments on commit dfafee8

Please sign in to comment.