From e1334ccae76ad04f9870c125875523a67d8953b2 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Fri, 4 Oct 2024 16:07:21 +0100 Subject: [PATCH] Added Replace with ofT and inex --- Toolkit.Foundation/ObservableCollection.cs | 26 ++++++++++++++++++++++ Toolkit.Foundation/RemoveAt.cs | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index 2021c70..31a9804 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -524,6 +524,32 @@ public partial class ObservableCollection : Activate(); } + public bool Replace(int index, + params object?[] parameters) + where T : + TViewModel + { + if (index <= Count - 1) + { + RemoveItem(index); + } + else + { + index = Count; + } + + T? item = Factory.Create(args => + { + if (args is IInitialization initialization) + { + initialization.Initialize(); + } + }, parameters); + + Insert(index, item); + return true; + } + public TViewModel Insert(int index = 0, params object?[] parameters) where T : diff --git a/Toolkit.Foundation/RemoveAt.cs b/Toolkit.Foundation/RemoveAt.cs index 77da9e1..e675135 100644 --- a/Toolkit.Foundation/RemoveAt.cs +++ b/Toolkit.Foundation/RemoveAt.cs @@ -2,6 +2,6 @@ public record RemoveAt { - public static RemoveAtEventArgs As(int index) => + public static RemoveAtEventArgs As(int index) => new(index); } \ No newline at end of file