Browse Source

PropertyGrid: working on Collection editor. This editor will only be able to add/remove items from a collection of IList.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
a17f24e2bb
  1. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs
  2. 68
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml
  3. 166
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditorDialog.xaml.cs

17
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();
}
}

68
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">
<Grid>
Title="Collection Editor" Height="400" Width="600" WindowStartupLocation="CenterScreen">
<Window.CommandBindings>
<CommandBinding Command="New" Executed="AddNew" CanExecute="CanAddNew" />
<CommandBinding Command="Delete" Executed="Delete" CanExecute="CanDelete" />
<CommandBinding Command="ComponentCommands.MoveDown" Executed="MoveDown" CanExecute="CanMoveDown" />
<CommandBinding Command="ComponentCommands.MoveUp" Executed="MoveUp" CanExecute="CanMoveUp" />
</Window.CommandBindings>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
@ -11,18 +19,62 @@
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="_listBox" />
<Grid Margin="0,0,10,0" MinWidth="200">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<propertyGrid:PropertyGrid x:Name="_propertyGrid" Grid.Column="1" SelectedObject="{Binding SelectedItem, ElementName=_listBox}" />
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<StackPanel>
<TextBlock Text="Select Type:" Margin="4" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox x:Name="_newItemTypes" Margin="0,0,4,0"
ItemsSource="{Binding NewItemTypes}"
DisplayMemberPath="Name"
SelectedIndex="0"/>
<Button Content="Add" Grid.Column="1" Width="50"
Command="New" CommandParameter="{Binding SelectedItem, ElementName=_newItemTypes}"/>
</Grid>
</StackPanel>
<ListBox x:Name="_listBox" Grid.Row="1" Margin="0,4,0,0"
ItemsSource="{Binding Items}"
SelectedIndex="0"
SelectedItem="{Binding SelectedItem}"/>
<StackPanel Grid.Column="1" Grid.Row="1" Margin="5,4,0,0">
<Button Command="ComponentCommands.MoveUp" CommandParameter="{Binding SelectedItem, ElementName=_listBox}">Up</Button>
<Button Command="ComponentCommands.MoveDown" CommandParameter="{Binding SelectedItem, ElementName=_listBox}">Down</Button>
<Button Command="Delete" CommandParameter="{Binding SelectedItem, ElementName=_listBox}" >Delete</Button>
</StackPanel>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Properties:" Margin="4" />
<propertyGrid:PropertyGrid x:Name="_propertyGrid" Grid.Row="1" SelectedObject="{Binding SelectedItem}" />
</Grid>
</Grid>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right" Margin="5" >
<Button Width="65" Margin="2" Click="Button_Click" >OK</Button>
<!--<Button Width="65" Margin="2">Cancel</Button>-->
<Button Width="75" Margin="2" Click="OkButton_Click" IsDefault="True">OK</Button>
<Button Width="75" Margin="2" IsCancel="True">Cancel</Button>
</StackPanel>
</Grid>

166
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
/// </summary>
public partial class CollectionEditorDialog : Window
{
PropertyItem _item;
#region Properties
public CollectionEditorDialog()
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<object>), typeof(CollectionEditorDialog), new UIPropertyMetadata(null));
public ObservableCollection<object> Items
{
get { return (ObservableCollection<object>)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<object>();
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<Type> NewItemTypes
{
get { return (IList<Type>)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<Type> GetNewItemTypes(Type type)
{
List<Type> types = new List<Type>();
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
}
}

Loading…
Cancel
Save