More selection edge cases fixed

This commit is contained in:
TheXamlGuy
2024-05-21 22:38:11 +01:00
parent fda5e7db6c
commit 4c2bc8e628
5 changed files with 32 additions and 14 deletions
+19 -1
View File
@@ -29,7 +29,8 @@ public partial class ObservableCollection<TItem> :
INotificationHandler<InsertEventArgs<TItem>>, INotificationHandler<InsertEventArgs<TItem>>,
INotificationHandler<MoveEventArgs<TItem>>, INotificationHandler<MoveEventArgs<TItem>>,
INotificationHandler<MoveToEventArgs<TItem>>, INotificationHandler<MoveToEventArgs<TItem>>,
INotificationHandler<ReplaceEventArgs<TItem>> INotificationHandler<ReplaceEventArgs<TItem>>,
INotificationHandler<SelectionEventArgs<TItem>>
where TItem : where TItem :
IDisposable IDisposable
{ {
@@ -43,6 +44,9 @@ public partial class ObservableCollection<TItem> :
[ObservableProperty] [ObservableProperty]
private int selectedIndex = 0; private int selectedIndex = 0;
[ObservableProperty]
private TItem? selectedItem;
public ObservableCollection(IServiceProvider provider, public ObservableCollection(IServiceProvider provider,
IServiceFactory factory, IServiceFactory factory,
IMediator mediator, IMediator mediator,
@@ -279,6 +283,15 @@ public partial class ObservableCollection<TItem> :
if (args.Value is TItem item) if (args.Value is TItem item)
{ {
Insert(args.Index, item); Insert(args.Index, item);
if (item is ISelectable selectable)
{
if (selectable.Selected)
{
SelectedItem = item;
SelectedIndex = this.IndexOf(item);
}
}
} }
return Task.CompletedTask; return Task.CompletedTask;
@@ -487,6 +500,11 @@ public partial class ObservableCollection<TItem> :
added.Selected = true; added.Selected = true;
} }
} }
public Task Handle(SelectionEventArgs<TItem> args)
{
return Task.CompletedTask;
}
} }
public partial class ObservableCollection<TValue, TViewModel>(IServiceProvider provider, public partial class ObservableCollection<TValue, TViewModel>(IServiceProvider provider,
-10
View File
@@ -1,10 +0,0 @@
namespace Toolkit.Foundation;
public record Selected
{
public static SelectedEventArgs<TValue> As<TValue>(TValue value) =>
new(value);
public static SelectedEventArgs<TValue> As<TValue>() where TValue : new() =>
new(new TValue());
}
-3
View File
@@ -1,3 +0,0 @@
namespace Toolkit.Foundation;
public record SelectedEventArgs<TValue>(TValue? Value);
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public record Selection
{
public static SelectionEventArgs<TValue> As<TValue>(TValue value) =>
new(value);
public static SelectionEventArgs<TValue> As<TValue>() where TValue : new() =>
new(new TValue());
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record SelectionEventArgs<TValue>(TValue? Value);