-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from column-visibility
added Visibility proeprty for columns
- Loading branch information
Showing
5 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters