diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs index 20a8aaba..8ab5965d 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs @@ -1,16 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Windows; using System.Windows.Controls; +using System.Collections; using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; namespace Microsoft.Windows.Controls.PropertyGrid.Editors { @@ -38,7 +30,12 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors private void Button_Click(object sender, RoutedEventArgs e) { - CollectionEditorDialog editor = new CollectionEditorDialog(_item); + CollectionEditorDialog editor = new CollectionEditorDialog(_item.PropertyType); + + Binding binding = new Binding("Value"); + binding.Source = _item; + binding.Mode = _item.IsWriteable ? BindingMode.TwoWay : BindingMode.OneWay; + BindingOperations.SetBinding(editor, CollectionEditorDialog.ItemsSourceProperty, binding); editor.ShowDialog(); } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml index 32a5e84d..53db2ab2 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml @@ -2,8 +2,16 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:propertyGrid="clr-namespace:Microsoft.Windows.Controls.PropertyGrid" - Title="Collection Editor" Height="400" Width="800" WindowStartupLocation="CenterScreen"> - + Title="Collection Editor" Height="400" Width="600" WindowStartupLocation="CenterScreen"> + + + + + + + + + @@ -11,18 +19,62 @@ - + - + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - + + diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml.cs index 17d92e2e..594ad50b 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml.cs @@ -11,6 +11,9 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Collections; +using System.Collections.ObjectModel; +using System.Reflection; +using System.ComponentModel; namespace Microsoft.Windows.Controls.PropertyGrid.Editors { @@ -19,30 +22,167 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors /// public partial class CollectionEditorDialog : Window { - PropertyItem _item; + #region Properties - public CollectionEditorDialog() + public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection), typeof(CollectionEditorDialog), new UIPropertyMetadata(null)); + public ObservableCollection Items + { + get { return (ObservableCollection)GetValue(ItemsProperty); } + set { SetValue(ItemsProperty, value); } + } + + public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CollectionEditorDialog), new UIPropertyMetadata(null, OnItemsSourceChanged)); + public IEnumerable ItemsSource + { + get { return (IEnumerable)GetValue(ItemsSourceProperty); } + set { SetValue(ItemsSourceProperty, value); } + } + + private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + CollectionEditorDialog collectionEditor = (CollectionEditorDialog)d; + if (collectionEditor != null) + collectionEditor.OnItemSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); + } + + public void OnItemSourceChanged(IEnumerable oldValue, IEnumerable newValue) + { + if (newValue != null) + { + Items = new ObservableCollection(); + foreach (var item in newValue) + { + object clone = Activator.CreateInstance(item.GetType()); + CopyValues(item, clone); + Items.Add(clone); + } + } + } + + public static readonly DependencyProperty NewItemTypesProperty = DependencyProperty.Register("NewItemTypes", typeof(IList), typeof(CollectionEditorDialog), new UIPropertyMetadata(null)); + public IList NewItemTypes + { + get { return (IList)GetValue(NewItemTypesProperty); } + set { SetValue(NewItemTypesProperty, value); } + } + + public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(CollectionEditorDialog), new UIPropertyMetadata(null)); + public object SelectedItem + { + get { return (object)GetValue(SelectedItemProperty); } + set { SetValue(SelectedItemProperty, value); } + } + + #endregion //Properties + + #region Constructors + + private CollectionEditorDialog() { InitializeComponent(); + DataContext = this; } - public CollectionEditorDialog(PropertyItem item) + public CollectionEditorDialog(Type type) : this() { - _item = item; - _listBox.ItemsSource = _item.Value as IEnumerable; + NewItemTypes = GetNewItemTypes(type); } - //public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(CollectionEditorDialog), new UIPropertyMetadata(null)); - //public object ItemsSource - //{ - // get { return (object)GetValue(ItemsSourceProperty); } - // set { SetValue(ItemsSourceProperty, value); } - //} + #endregion //Constructors - private void Button_Click(object sender, RoutedEventArgs e) + private void OkButton_Click(object sender, RoutedEventArgs e) { + PersistChanges(); Close(); - } + } + + private static void CopyValues(object source, object destination) + { + FieldInfo[] myObjectFields = source.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + foreach (FieldInfo fi in myObjectFields) + { + fi.SetValue(destination, fi.GetValue(source)); + } + } + + private static List GetNewItemTypes(Type type) + { + List types = new List(); + var newItemTypes = type.GetGenericArguments(); + foreach (var t in newItemTypes) + { + types.Add(t); + } + return types; + } + + private void PersistChanges() + { + if (ItemsSource is IList) + { + IList list = (IList)ItemsSource; + //Need to copy all changes into ItemsSource + } + } + + #region Commands + + private void AddNew(object sender, ExecutedRoutedEventArgs e) + { + Type t = (Type)e.Parameter; + var newItem = Activator.CreateInstance(t); + Items.Add(newItem); + SelectedItem = newItem; + } + + private void CanAddNew(object sender, CanExecuteRoutedEventArgs e) + { + Type t = e.Parameter as Type; + if (t != null && t.IsClass) + e.CanExecute = true; + } + + private void Delete(object sender, ExecutedRoutedEventArgs e) + { + Items.Remove(e.Parameter); + } + + private void CanDelete(object sender, CanExecuteRoutedEventArgs e) + { + e.CanExecute = e.Parameter != null; + } + + private void MoveDown(object sender, ExecutedRoutedEventArgs e) + { + var selectedItem = e.Parameter; + var index = Items.IndexOf(selectedItem); + Items.RemoveAt(index); + Items.Insert(++index, selectedItem); + SelectedItem = selectedItem; + } + + private void CanMoveDown(object sender, CanExecuteRoutedEventArgs e) + { + if (e.Parameter != null && Items.IndexOf(e.Parameter) < (Items.Count - 1)) + e.CanExecute = true; + } + + private void MoveUp(object sender, ExecutedRoutedEventArgs e) + { + var selectedItem = e.Parameter; + var index = Items.IndexOf(selectedItem); + Items.RemoveAt(index); + Items.Insert(--index, selectedItem); + SelectedItem = selectedItem; + } + + private void CanMoveUp(object sender, CanExecuteRoutedEventArgs e) + { + if (e.Parameter != null && Items.IndexOf(e.Parameter) > 0) + e.CanExecute = true; + } + + #endregion //Commands } }