From f326e569a14fb417c6c032586da1c8510d00c2a5 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Thu, 8 Feb 2018 23:34:44 -0500 Subject: [PATCH 1/2] Add ObjectCollectionEditor which allows PropertyGrids to edit any collection of arbitrary objects using yet another PropertyGrid --- Torch/Torch.csproj | 7 ++ Torch/Views/CollectionEditor.xaml.cs | 23 +++++ Torch/Views/ObjectCollectionEditor.xaml | 20 ++++ Torch/Views/ObjectCollectionEditor.xaml.cs | 114 +++++++++++++++++++++ Torch/Views/PropertyGrid.xaml.cs | 36 +++++++ 5 files changed, 200 insertions(+) create mode 100644 Torch/Views/ObjectCollectionEditor.xaml create mode 100644 Torch/Views/ObjectCollectionEditor.xaml.cs diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index b5352d4..29d220e 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -260,6 +260,9 @@ DictionaryEditor.xaml + + ObjectCollectionEditor.xaml + PropertyGrid.xaml @@ -284,6 +287,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + MSBuild:Compile Designer diff --git a/Torch/Views/CollectionEditor.xaml.cs b/Torch/Views/CollectionEditor.xaml.cs index e1b81f8..d2e3bac 100644 --- a/Torch/Views/CollectionEditor.xaml.cs +++ b/Torch/Views/CollectionEditor.xaml.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -21,11 +22,33 @@ namespace Torch.Views /// public partial class CollectionEditor : Window { + private static readonly Dictionary MethodCache = new Dictionary(); + private static readonly MethodInfo EditMethod; + public CollectionEditor() { InitializeComponent(); } + static CollectionEditor() + { + var m = typeof(CollectionEditor).GetMethods(); + EditMethod = m.First(mt => mt.Name == "Edit" && mt.GetGenericArguments().Length == 1); + } + + public void Edit(ICollection collection, string name) + { + var gt = collection.GetType().GenericTypeArguments[0]; + MethodInfo gm; + if (!MethodCache.TryGetValue(gt, out gm)) + { + gm = EditMethod.MakeGenericMethod(gt); + MethodCache.Add(gt, gm); + } + + gm.Invoke(this, new object[] {collection, name}); + } + public void Edit(ICollection collection, string name) { ItemList.Text = string.Join("\r\n", collection.Select(x => x.ToString())); diff --git a/Torch/Views/ObjectCollectionEditor.xaml b/Torch/Views/ObjectCollectionEditor.xaml new file mode 100644 index 0000000..72f899f --- /dev/null +++ b/Torch/Views/ObjectCollectionEditor.xaml @@ -0,0 +1,20 @@ + + + + +