From cce1501319c5e068956a42506f753b7b8b3eb77b Mon Sep 17 00:00:00 2001 From: billw2012 Date: Tue, 27 Jul 2021 20:14:47 +0100 Subject: [PATCH] Add support for ICloneable to the CollectionControl --- ...PropertyGridDefaultEditorsView.xaml.cs.txt | 8 ++++- .../PropertyGridDefaultEditorsView.xaml.cs | 8 ++++- .../Implementation/CollectionControl.cs | 30 ++++++++++++------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/CodeFiles/PropertyGridDefaultEditorsView.xaml.cs.txt b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/CodeFiles/PropertyGridDefaultEditorsView.xaml.cs.txt index 55921600c..b4d01ebe4 100644 --- a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/CodeFiles/PropertyGridDefaultEditorsView.xaml.cs.txt +++ b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/CodeFiles/PropertyGridDefaultEditorsView.xaml.cs.txt @@ -167,9 +167,15 @@ namespace Xceed.Wpf.Toolkit.LiveExplorer.Samples.PropertyGrid.Views public Person Person { get; set; } } - public class Person + public class Person : ICloneable { + [ReadOnly(true)] + public Guid UniqueID { get; set; } = Guid.NewGuid(); public string Name { get; set; } + public object Clone() + { + return new Person { Name = Name }; + } } } } diff --git a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/Samples/PropertyGrid/Views/PropertyGridDefaultEditorsView.xaml.cs b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/Samples/PropertyGrid/Views/PropertyGridDefaultEditorsView.xaml.cs index 55921600c..b4d01ebe4 100644 --- a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/Samples/PropertyGrid/Views/PropertyGridDefaultEditorsView.xaml.cs +++ b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit.LiveExplorer/Samples/PropertyGrid/Views/PropertyGridDefaultEditorsView.xaml.cs @@ -167,9 +167,15 @@ public Collection CollectionOfPerson public Person Person { get; set; } } - public class Person + public class Person : ICloneable { + [ReadOnly(true)] + public Guid UniqueID { get; set; } = Guid.NewGuid(); public string Name { get; set; } + public object Clone() + { + return new Person { Name = Name }; + } } } } diff --git a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/CollectionControl.cs b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/CollectionControl.cs index 0f7b04a88..7b7759ca7 100644 --- a/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/CollectionControl.cs +++ b/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/CollectionControl/Implementation/CollectionControl.cs @@ -539,20 +539,28 @@ private object DuplicateItem( ExecutedRoutedEventArgs e ) var baseItem = e.Parameter; var newItemType = baseItem.GetType(); - var newItem = this.CreateNewItem( newItemType ); - - var type = newItemType; - while( type != null ) + if (typeof(ICloneable).IsAssignableFrom(newItemType)) { - var baseProperties = type.GetFields( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance ); - foreach( var prop in baseProperties ) - { - prop.SetValue( newItem, prop.GetValue( baseItem ) ); - } - type = type.BaseType; + return ((ICloneable) baseItem).Clone(); } + else + { + var newItem = this.CreateNewItem(newItemType); - return newItem; + var type = newItemType; + while (type != null) + { + var baseProperties = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + foreach (var prop in baseProperties) + { + prop.SetValue(newItem, prop.GetValue(baseItem)); + } + + type = type.BaseType; + } + + return newItem; + } } private void MoveDown( object sender, ExecutedRoutedEventArgs e )