Skip to content

Commit

Permalink
fix: properly sync columns
Browse files Browse the repository at this point in the history
  • Loading branch information
giard-alexandre committed Oct 1, 2024
1 parent fc7eaaf commit 133fa57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/DynamicTreeDataGrid/Controls/ColumnEditorWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
xmlns:controls="clr-namespace:DynamicTreeDataGrid.Controls"
mc:Ignorable="d" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterOwner"
x:Class="DynamicTreeDataGrid.Controls.ColumnEditorWindow"
Title="ColumnEditorWindow" MinWidth="300">
Title="Column Editor" MinWidth="300">
<controls:ColumnListView />
</Window>
45 changes: 30 additions & 15 deletions src/DynamicTreeDataGrid/Models/Columns/DynamicColumnList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace DynamicTreeDataGrid.Models.Columns;
/// <typeparam name="TModel"></typeparam>
public class DynamicColumnList<TModel> : DynamicColumnListBase<TModel>, IDynamicColumns
where TModel : class {
private readonly DynamicColumnListBase<TModel> _displayedColumns = [];
private readonly DynamicColumnListBase<TModel> _displayedColumns = [];

/// <summary>
/// Columns that are used by the TreeDataGrid. Filters out the columns that should not be visible.
/// </summary>
public IDynamicColumnsBase DisplayedColumns => _displayedColumns;
/// <summary>
/// Columns that are used by the TreeDataGrid. Filters out the columns that should not be visible.
/// </summary>
public IDynamicColumnsBase DisplayedColumns => _displayedColumns;

public DynamicColumnList() {
this.CollectionChanged += SyncFilteredCollection;
Expand Down Expand Up @@ -83,9 +83,11 @@ private void ItemAdded(object? item) {

private void ItemAdded(IDynamicColumn<TModel> column) {
column.PropertyChanged += Item_PropertyChanged;
if (column.Visible) {
_displayedColumns.Add(column);
}
if (!column.Visible) return;

var index = IndexOf(column);
var offset = GetDisplayedOffset(index);
_displayedColumns.Insert(index - offset, column);
}

private void ItemRemoved(object? item) {
Expand All @@ -105,14 +107,27 @@ private void ItemReplaced(NotifyCollectionChangedEventArgs e) {
// Replace in the filtered list too (somehow)
}

private void Item_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (sender is IDynamicColumn<TModel> column && e.PropertyName == nameof(IDynamicColumn.Visible)) {
if (column.Visible && !_displayedColumns.Contains(column)) {
_displayedColumns.Add(column);
}
else {
_displayedColumns.Remove(column);
private int GetDisplayedOffset(int desiredIndex) {
var indexOffset = 0;
for (int i = 0; i < desiredIndex; i++) {
if (!this[i].Visible) {
indexOffset++;
}
}

return indexOffset;
}

private void Item_PropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (sender is not IDynamicColumn<TModel> column || e.PropertyName != nameof(IDynamicColumn.Visible)) return;

if (column.Visible && !_displayedColumns.Contains(column)) {
var index = IndexOf(column);
var offset = GetDisplayedOffset(index);
_displayedColumns.Insert(index - offset, column);
}
else {
_displayedColumns.Remove(column);
}
}
}

0 comments on commit 133fa57

Please sign in to comment.