// ********************************************************************* // PLEASE DO NOT REMOVE THIS DISCLAIMER // // WpfPropertyGrid - By Jaime Olivares // Article site: http://www.codeproject.com/KB/grid/WpfPropertyGrid.aspx // Author site: www.jaimeolivares.com // License: Code Project Open License (CPOL) // // ********************************************************************* using System.Activities; using System.Activities.Presentation; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Windows.Controls; using System.Linq; namespace System.Windows.Control { /// /// WPF Native PropertyGrid class, taken from Workflow Foundation Designer /// public class WpfPropertyGrid : Grid { #region Private fields private WorkflowDesigner Designer; private DummyActivity UniqueActivity; private MethodInfo RefreshMethod; private TextBlock SelectionTypeLabel; #endregion #region Public properties /// /// Facade for the dummy activity's SelectedObject /// public object SelectedObject { get { return this.UniqueActivity.SelectedObject; } set { this.UniqueActivity.SelectedObject = value; this.RefreshPropertyList(); this.SelectionTypeLabel.Text = value == null ? string.Empty : value.GetType().Name; } } /// /// XAML information with PropertyGrid's font and color information /// /// Documentation for WorkflowDesigner.PropertyInspectorFontAndColorData public string FontAndColorData { set { Designer.PropertyInspectorFontAndColorData = value; } } #endregion /// /// Default constructor, creates a hidden designer view and a property inspector /// public WpfPropertyGrid() { this.UniqueActivity = new DummyActivity(); this.Designer = new WorkflowDesigner(); Designer.PropertyInspectorView.Visibility = Visibility.Visible; Designer.View.Visibility = Windows.Visibility.Hidden; this.Children.Add(Designer.PropertyInspectorView); this.Children.Add(Designer.View); Designer.Load(this.UniqueActivity); this.RefreshMethod = Designer.PropertyInspectorView.GetType().GetMethod("RefreshPropertyList", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance | Reflection.BindingFlags.DeclaredOnly); this.SelectionTypeLabel = this.Designer.PropertyInspectorView.GetType().GetMethod("get_SelectionTypeLabel", Reflection.BindingFlags.Public | Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance | Reflection.BindingFlags.DeclaredOnly).Invoke(Designer.PropertyInspectorView, new object[0]) as TextBlock; this.SelectionTypeLabel.Text = string.Empty; } /// /// Updates the PropertyGrid's properties /// public void RefreshPropertyList() { RefreshMethod.Invoke(Designer.PropertyInspectorView, new object[] { false }); } /// /// Dummy activity, will contain the selected object. Will be exposed in the PropertyGrid /// public sealed class DummyActivity : CodeActivity, ICustomTypeDescriptor { /// /// Storage of the Selected Object /// [Browsable(false)] public object SelectedObject { get; set; } /// /// Override to hide this property /// [Browsable(false)] public new string DisplayName { get; set; } /// /// Not used, since a workflow instance is never created /// /// Not used protected override void Execute(CodeActivityContext context) { // Do nothing } /// /// Default constructor /// public DummyActivity() { } /// /// Type descriptor methods. All them will be redirected to expose SelectedObject's type description /// /// #region ICustomTypeDescriptor Members public string GetClassName() { return SelectedObject == null ? null : TypeDescriptor.GetClassName(SelectedObject); } public AttributeCollection GetAttributes() { return SelectedObject == null ? null : TypeDescriptor.GetAttributes(SelectedObject); } public string GetComponentName() { return SelectedObject == null ? null : TypeDescriptor.GetComponentName(SelectedObject); } public TypeConverter GetConverter() { return SelectedObject == null ? null : TypeDescriptor.GetConverter(SelectedObject); } public EventDescriptor GetDefaultEvent() { return SelectedObject == null ? null : TypeDescriptor.GetDefaultEvent(SelectedObject); } public PropertyDescriptor GetDefaultProperty() { return SelectedObject == null ? null : TypeDescriptor.GetDefaultProperty(SelectedObject); } public object GetEditor(Type editorBaseType) { return SelectedObject == null ? null : TypeDescriptor.GetEditor(SelectedObject, editorBaseType); } public EventDescriptorCollection GetEvents(Attribute[] attributes) { return SelectedObject == null ? null : TypeDescriptor.GetEvents(SelectedObject, attributes); } public EventDescriptorCollection GetEvents() { return SelectedObject == null ? null : TypeDescriptor.GetEvents(SelectedObject); } public object GetPropertyOwner(PropertyDescriptor pd) { return SelectedObject == null ? null : this; } public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); } // returns the properties of the selected object, instead of the activity itself public PropertyDescriptorCollection GetProperties() { if (SelectedObject == null) { return TypeDescriptor.GetProperties(this, new Attribute[] { new BrowsableAttribute(false) }, true); } var props = new PropertyDescriptorCollection(null); if (SelectedObject is ICustomTypeDescriptor) { foreach (PropertyDescriptor prop in (SelectedObject as ICustomTypeDescriptor).GetProperties()) { props.Add(new WPG_PropertyDescriptor(this, prop.Name)); } } else { foreach (var prop in SelectedObject.GetType().GetProperties()) { props.Add(new WPG_PropertyDescriptor(this, prop.Name)); } } return props; } #endregion } /// /// Property descriptor for the SelectedObject. Will expose SelectedObject properties as if they were DummyActivity properties /// public class WPG_PropertyDescriptor : PropertyDescriptor { #region Private fields private DummyActivity OwnerActivity = null; private string PropName = null; private PropertyInfo PropInfo = null; #endregion public WPG_PropertyDescriptor(DummyActivity owner, string name) : base(name, null) { this.OwnerActivity = owner; this.PropName = name; this.PropInfo = OwnerActivity.SelectedObject.GetType().GetProperty(name); } public override AttributeCollection Attributes { get { List attrs = new List(); foreach (var attrInfo in PropInfo.GetCustomAttributes(true)) { attrs.Add(attrInfo as Attribute); } return new AttributeCollection(attrs.ToArray()); } } public override bool CanResetValue(object component) { return false; } public override Type ComponentType { get { return this.OwnerActivity.GetType(); } } public override string DisplayName { get { return this.PropName; } } public override string Description { get { return this.PropName; } } public override bool IsReadOnly { get { return false; } } public override string Name { get { return this.PropName; } } public override Type PropertyType { get { return PropInfo.PropertyType; } } public override void ResetValue(object component) { } public override bool ShouldSerializeValue(object component) { return true; } public override object GetValue(object component) { return PropInfo.GetValue(this.OwnerActivity.SelectedObject, null); } public override void SetValue(object component, object value) { PropInfo.SetValue(this.OwnerActivity.SelectedObject, value, null); } } } }