Browse Source

PropertyGrid: implemented SelectedProperty.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
68a8143c63
  1. 119
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs
  2. 107
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs
  3. 23
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml

119
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs

@ -8,6 +8,7 @@ using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders;
using System.Windows.Input;
namespace Microsoft.Windows.Controls.PropertyGrid
{
@ -41,54 +42,30 @@ namespace Microsoft.Windows.Controls.PropertyGrid
protected virtual void OnIsCategorizedChanged(bool oldValue, bool newValue)
{
LoadProperties(newValue);
}
}
#endregion //IsCategorized
#region NameWidth
public static readonly DependencyProperty NameWidthProperty = DependencyProperty.Register("NameWidth", typeof(double), typeof(PropertyGrid), new UIPropertyMetadata(120.0, OnNameWidthChanged));
public double NameWidth
public static readonly DependencyProperty NameColumnWidthProperty = DependencyProperty.Register("NameColumnWidth", typeof(double), typeof(PropertyGrid), new UIPropertyMetadata(120.0));
public double NameColumnWidth
{
get { return (double)GetValue(NameWidthProperty); }
set { SetValue(NameWidthProperty, value); }
}
private static void OnNameWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyGrid propertyGrid = o as PropertyGrid;
if (propertyGrid != null)
propertyGrid.OnNameWidthChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnNameWidthChanged(double oldValue, double newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
get { return (double)GetValue(NameColumnWidthProperty); }
set { SetValue(NameColumnWidthProperty, value); }
}
#endregion //NameWidth
#region Properties
public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(PropertyCollection), typeof(PropertyGrid), new UIPropertyMetadata(null, OnPropertiesChanged));
public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(PropertyCollection), typeof(PropertyGrid), new UIPropertyMetadata(null));
public PropertyCollection Properties
{
get { return (PropertyCollection)GetValue(PropertiesProperty); }
private set { SetValue(PropertiesProperty, value); }
}
private static void OnPropertiesChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyGrid propertyGrid = o as PropertyGrid;
if (propertyGrid != null)
propertyGrid.OnPropertiesChanged((PropertyCollection)e.OldValue, (PropertyCollection)e.NewValue);
}
protected virtual void OnPropertiesChanged(PropertyCollection oldValue, PropertyCollection newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //Properties
#region SelectedObject
@ -127,7 +104,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid
public Type SelectedObjectType
{
get { return (Type)GetValue(SelectedObjectTypeProperty); }
set { SetValue(SelectedObjectTypeProperty, value); }
private set { SetValue(SelectedObjectTypeProperty, value); }
}
private static void OnSelectedObjectTypeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
@ -146,49 +123,52 @@ namespace Microsoft.Windows.Controls.PropertyGrid
#region SelectedObjectTypeName
public static readonly DependencyProperty SelectedObjectTypeNameProperty = DependencyProperty.Register("SelectedObjectTypeName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty, OnSelectedObjectTypeNameChanged));
public static readonly DependencyProperty SelectedObjectTypeNameProperty = DependencyProperty.Register("SelectedObjectTypeName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty));
public string SelectedObjectTypeName
{
get { return (string)GetValue(SelectedObjectTypeNameProperty); }
set { SetValue(SelectedObjectTypeNameProperty, value); }
}
private static void OnSelectedObjectTypeNameChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyGrid propertyGrid = o as PropertyGrid;
if (propertyGrid != null)
propertyGrid.OnSelectedObjectTypeNameChanged((string)e.OldValue, (string)e.NewValue);
}
protected virtual void OnSelectedObjectTypeNameChanged(string oldValue, string newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
private set { SetValue(SelectedObjectTypeNameProperty, value); }
}
#endregion //SelectedObjectTypeName
#region SelectedObjectName
public static readonly DependencyProperty SelectedObjectNameProperty = DependencyProperty.Register("SelectedObjectName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty, OnSelectedObjectNameChanged));
public static readonly DependencyProperty SelectedObjectNameProperty = DependencyProperty.Register("SelectedObjectName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty));
public string SelectedObjectName
{
get { return (string)GetValue(SelectedObjectNameProperty); }
set { SetValue(SelectedObjectNameProperty, value); }
private set { SetValue(SelectedObjectNameProperty, value); }
}
#endregion //SelectedObjectName
#region SelectedProperty
public static readonly DependencyProperty SelectedPropertyProperty = DependencyProperty.Register("SelectedProperty", typeof(PropertyItem), typeof(PropertyGrid), new UIPropertyMetadata(null, OnSelectedPropertyChanged));
public PropertyItem SelectedProperty
{
get { return (PropertyItem)GetValue(SelectedPropertyProperty); }
internal set { SetValue(SelectedPropertyProperty, value); }
}
private static void OnSelectedObjectNameChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
private static void OnSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyGrid propertyGrid = o as PropertyGrid;
if (propertyGrid != null)
propertyGrid.OnSelectedObjectNameChanged((string)e.OldValue, (string)e.NewValue);
propertyGrid.OnSelectedPropertyChanged((PropertyItem)e.OldValue, (PropertyItem)e.NewValue);
}
protected virtual void OnSelectedObjectNameChanged(string oldValue, string newValue)
protected virtual void OnSelectedPropertyChanged(PropertyItem oldValue, PropertyItem newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
if (oldValue != null)
oldValue.IsSelected = false;
#endregion //SelectedObjectName
if (newValue != null)
newValue.IsSelected = true;
}
#endregion //SelectedProperty
#endregion //Properties
@ -211,13 +191,23 @@ namespace Microsoft.Windows.Controls.PropertyGrid
_dragThumb.DragDelta += DragThumb_DragDelta;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
//hitting enter on textbox will update value of underlying source
if (this.SelectedProperty != null && e.Key == Key.Enter && e.OriginalSource is TextBox)
{
BindingExpression be = ((TextBox)e.OriginalSource).GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}
}
#endregion //Base Class Overrides
#region Event Handlers
void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
NameWidth = Math.Max(0, NameWidth + e.HorizontalChange);
NameColumnWidth = Math.Max(0, NameColumnWidth + e.HorizontalChange);
}
#endregion //Event Handlers
@ -232,7 +222,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid
Properties = GetAlphabetizedProperties(_propertyItemsCache);
}
private static List<PropertyItem> GetObjectProperties(object instance)
private List<PropertyItem> GetObjectProperties(object instance)
{
var propertyItems = new List<PropertyItem>();
if (instance == null)
@ -243,14 +233,14 @@ namespace Microsoft.Windows.Controls.PropertyGrid
// Get all properties of the type
propertyItems.AddRange(properties.Cast<PropertyDescriptor>().
Where(p => p.IsBrowsable && p.Name != "GenericParameterAttributes").
Select(property => CreatePropertyItem(property, instance)));
Select(property => CreatePropertyItem(property, instance, this)));
return propertyItems;
}
private static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance)
private static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid)
{
PropertyItem propertyItem = new PropertyItem(instance, property);
PropertyItem propertyItem = new PropertyItem(instance, property, grid);
ITypeEditorProvider editorProvider = null;
if (propertyItem.PropertyType == typeof(string))
@ -275,20 +265,18 @@ namespace Microsoft.Windows.Controls.PropertyGrid
return propertyItem;
}
private PropertyCollection GetCategorizedProperties(List<PropertyItem> propertyItems)
private static PropertyCollection GetCategorizedProperties(List<PropertyItem> propertyItems)
{
PropertyCollection propertyCollection = new PropertyCollection();
CollectionViewSource src = new CollectionViewSource();
src.Source = propertyItems;
CollectionViewSource src = new CollectionViewSource { Source = propertyItems };
src.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
src.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
src.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
foreach (CollectionViewGroup item in src.View.Groups)
{
PropertyCategoryItem propertyCategoryItem = new PropertyCategoryItem();
propertyCategoryItem.Category = item.Name.ToString();
PropertyCategoryItem propertyCategoryItem = new PropertyCategoryItem { Category = item.Name.ToString() };
foreach (var propertyitem in item.Items)
{
propertyCategoryItem.Properties.Add((PropertyItem)propertyitem);
@ -299,15 +287,14 @@ namespace Microsoft.Windows.Controls.PropertyGrid
return propertyCollection;
}
private PropertyCollection GetAlphabetizedProperties(List<PropertyItem> propertyItems)
private static PropertyCollection GetAlphabetizedProperties(List<PropertyItem> propertyItems)
{
PropertyCollection propertyCollection = new PropertyCollection();
if (propertyItems == null)
return propertyCollection;
CollectionViewSource src = new CollectionViewSource();
src.Source = propertyItems;
CollectionViewSource src = new CollectionViewSource { Source = propertyItems };
src.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
foreach (var item in ((ListCollectionView)(src.View)))

107
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs

@ -7,104 +7,66 @@ namespace Microsoft.Windows.Controls.PropertyGrid
{
public class PropertyItem : Control
{
#region Members
public object Instance { get; private set; }
public PropertyDescriptor PropertyDescriptor { get; private set; }
#endregion //Members
#region Properties
public string Description
{
get { return PropertyDescriptor.Description; }
}
#region Category
public bool IsWriteable
public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(string), typeof(PropertyItem), new UIPropertyMetadata(string.Empty));
public string Category
{
get { return !IsReadOnly; }
get { return (string)GetValue(CategoryProperty); }
set { SetValue(CategoryProperty, value); }
}
public bool IsReadOnly
{
get { return PropertyDescriptor.IsReadOnly; }
}
#endregion //Category
public Type PropertyType
{
get { return PropertyDescriptor.PropertyType; }
}
public string Description { get { return PropertyDescriptor.Description; } }
//public string Category
//{
// get { return PropertyDescriptor.Category; }
//}
public object Instance { get; private set; }
public bool IsReadOnly { get { return PropertyDescriptor.IsReadOnly; } }
public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(string), typeof(PropertyItem), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnCategoryChanged), new CoerceValueCallback(OnCoerceCategory)));
#region IsSelected
private static object OnCoerceCategory(DependencyObject o, object value)
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(PropertyItem), new UIPropertyMetadata(false, OnIsSelectedChanged));
public bool IsSelected
{
PropertyItem propertyItem = o as PropertyItem;
if (propertyItem != null)
return propertyItem.OnCoerceCategory((string)value);
else
return value;
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
private static void OnCategoryChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
private static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyItem propertyItem = o as PropertyItem;
if (propertyItem != null)
propertyItem.OnCategoryChanged((string)e.OldValue, (string)e.NewValue);
propertyItem.OnIsSelectedChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual string OnCoerceCategory(string value)
protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue)
{
// TODO: Keep the proposed value within the desired range.
return value;
if (newValue)
PropertyGrid.SelectedProperty = this;
}
protected virtual void OnCategoryChanged(string oldValue, string newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //IsSelected
public string Category
{
// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
get
{
return (string)GetValue(CategoryProperty);
}
set
{
SetValue(CategoryProperty, value);
}
}
public bool IsWriteable { get { return !IsReadOnly; } }
public PropertyDescriptor PropertyDescriptor { get; private set; }
public PropertyGrid PropertyGrid { get; private set; }
public Type PropertyType { get { return PropertyDescriptor.PropertyType; } }
#region Editor
public static readonly DependencyProperty EditorProperty = DependencyProperty.Register("Editor", typeof(FrameworkElement), typeof(PropertyItem), new UIPropertyMetadata(null, OnEditorChanged));
public static readonly DependencyProperty EditorProperty = DependencyProperty.Register("Editor", typeof(FrameworkElement), typeof(PropertyItem), new UIPropertyMetadata(null));
public FrameworkElement Editor
{
get { return (FrameworkElement)GetValue(EditorProperty); }
set { SetValue(EditorProperty, value); }
}
private static void OnEditorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyItem propertyItem = o as PropertyItem;
if (propertyItem != null)
propertyItem.OnEditorChanged((FrameworkElement)e.OldValue, (FrameworkElement)e.NewValue);
}
protected virtual void OnEditorChanged(FrameworkElement oldValue, FrameworkElement newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //Editor
#endregion //Properties
@ -116,25 +78,24 @@ namespace Microsoft.Windows.Controls.PropertyGrid
DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyItem), new FrameworkPropertyMetadata(typeof(PropertyItem)));
}
public PropertyItem(object instance, PropertyDescriptor property)
public PropertyItem(object instance, PropertyDescriptor property, PropertyGrid propertyGrid)
{
Instance = instance;
PropertyDescriptor = property;
Name = PropertyDescriptor.Name;
Category = PropertyDescriptor.Category;
PropertyGrid = propertyGrid;
}
#endregion //Constructor
#region INotifyPropertyChanged Members
#region Base Class Overrides
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
IsSelected = true;
}
#endregion
#endregion //Base Class Overrides
}
}

23
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml

@ -102,13 +102,15 @@
<Style TargetType="{x:Type local:PropertyItem}">
<Setter Property="BorderBrush" Value="#FFF0F0F0" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Focusable" Value="False"/>
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:PropertyItem}">
<Border>
<Grid VerticalAlignment="Center" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NameWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PropertyGrid}}}"/>
<ColumnDefinition Width="{Binding NameColumnWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PropertyGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
@ -117,10 +119,19 @@
</Border>
<Border BorderThickness="0.5" BorderBrush="#FFF0F0F0" x:Name="PART_Editor" Grid.Column="1" Background="Transparent">
<ContentControl Content="{TemplateBinding Editor}" VerticalAlignment="Center" />
<ContentControl Content="{TemplateBinding Editor}" VerticalAlignment="Center" Focusable="False" IsTabStop="False" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_Name" Property="Background" Value="#CED4DF" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="PART_Name" Property="Background" Value="#43577B" />
<Setter TargetName="PART_Name" Property="TextElement.Foreground" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
@ -165,7 +176,7 @@
<Grid Grid.Row="2" Background="White">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl ItemsSource="{Binding Properties, RelativeSource={RelativeSource TemplatedParent}}" >
<ItemsControl ItemsSource="{Binding Properties, RelativeSource={RelativeSource TemplatedParent}}" IsTabStop="False" Focusable="False" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
@ -176,7 +187,7 @@
<Thumb x:Name="PART_DragThumb" HorizontalAlignment="Left" Width="5" Margin="6,0,0,0" Cursor="SizeWE">
<Thumb.RenderTransform>
<TranslateTransform X="{Binding NameWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PropertyGrid}}}" />
<TranslateTransform X="{Binding NameColumnWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:PropertyGrid}}}" />
</Thumb.RenderTransform>
<Thumb.Template>
<ControlTemplate>
@ -189,8 +200,8 @@
<GridSplitter Height="3" Grid.Row="3" HorizontalAlignment="Stretch" />
<StackPanel Grid.Row="4" Margin="0,0,0,5">
<TextBlock Padding="2 2 2 0" FontWeight="Bold" Text="Property Name" />
<TextBlock Padding="5 2 2 0" TextWrapping="WrapWithOverflow" Text="Property Description" />
<TextBlock Padding="2 2 2 0" FontWeight="Bold" Text="{Binding SelectedProperty.Name, RelativeSource={RelativeSource TemplatedParent}}" />
<TextBlock Padding="5 2 2 0" TextWrapping="WrapWithOverflow" Text="{Binding SelectedProperty.Description, RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
</Grid>
</Border>

Loading…
Cancel
Save