Skip to content

Commit

Permalink
throw exceptions if the user tries to move, replace, remove or add a …
Browse files Browse the repository at this point in the history
…second expander column.
  • Loading branch information
danwalmsley committed Oct 11, 2024
1 parent c45175f commit 04f57cc
Showing 1 changed file with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
Expand Down Expand Up @@ -346,18 +347,66 @@ private void OnColumnsCollectionChanged(object? sender, NotifyCollectionChangedE
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (_expanderColumn is null && e.NewItems is object)
HandleAdd(e.NewItems);
break;

case NotifyCollectionChangedAction.Remove:
HandleRemoveReplaceOrMove(e.OldItems, "removed");
break;

case NotifyCollectionChangedAction.Replace:
HandleRemoveReplaceOrMove(e.NewItems, "replaced");
break;

case NotifyCollectionChangedAction.Move:
HandleRemoveReplaceOrMove(e.NewItems, "moved");
break;

case NotifyCollectionChangedAction.Reset:
if (_expanderColumn is not null)
{
foreach (var i in e.NewItems)
throw new InvalidOperationException("The expander column cannot be removed by a reset.");
}

_expanderColumn = null; // Optionally clear the expander column
break;

default:
throw new ArgumentOutOfRangeException();
}
}

private void HandleAdd(IList? newItems)
{
if (newItems is not null)
{
foreach (var i in newItems)
{
if (i is IExpanderColumn<TModel> expander)
{
if (_expanderColumn is not null)
{
if (i is IExpanderColumn<TModel> expander)
{
_expanderColumn = expander;
break;
}
throw new InvalidOperationException("Only one expander column is allowed.");
}

_expanderColumn = expander;
break;
}
break;
}
}
}

private void HandleRemoveReplaceOrMove(IList? items, string action)
{
if (items is not null)
{
foreach (var i in items)
{
if (i is IExpanderColumn<TModel> && _expanderColumn is not null)
{
throw new InvalidOperationException($"The expander column cannot be {action}.");
}
}
}
}
}
Expand Down

0 comments on commit 04f57cc

Please sign in to comment.