Browse Source
public class ColorSource : IItemsSource
{
public IList<object> GetValues()
{
return new List<object>() { Colors.Blue, Colors.Gray, Colors.Green };
}
}
Then decorate your property with the ItemsSource attirbute and specify the Type of source to use:
private Color _color = Colors.Green;
[ItemsSource(typeof(ColorSource))]
public Color Color
{
get { return _color; }
set { _color = value; }
}
When the PropertyGrid loads, the editor for the Color property will be a ComboBox with an ItemsSource of ColorSource.GetValues().
pull/1645/head
5 changed files with 86 additions and 3 deletions
@ -0,0 +1,10 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Microsoft.Windows.Controls.PropertyGrid.Attributes |
||||
|
{ |
||||
|
public interface IItemsSource |
||||
|
{ |
||||
|
IList<object> GetValues(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Microsoft.Windows.Controls.PropertyGrid.Attributes |
||||
|
{ |
||||
|
public class ItemsSourceAttribute : Attribute |
||||
|
{ |
||||
|
public Type Type { get; set; } |
||||
|
|
||||
|
public ItemsSourceAttribute(Type type) |
||||
|
{ |
||||
|
var valueSourceInterface = type.GetInterface("Microsoft.Windows.Controls.PropertyGrid.Attributes.IItemsSource"); |
||||
|
if (valueSourceInterface == null) |
||||
|
throw new ArgumentException("Type must implement the IItemsSource interface.", "type"); |
||||
|
|
||||
|
Type = type; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using Microsoft.Windows.Controls.PropertyGrid.Attributes; |
||||
|
|
||||
|
namespace Microsoft.Windows.Controls.PropertyGrid.Editors |
||||
|
{ |
||||
|
public class ItemsSourceEditor : ComboBoxEditor |
||||
|
{ |
||||
|
private ItemsSourceAttribute _attribute; |
||||
|
|
||||
|
public ItemsSourceEditor(ItemsSourceAttribute attribute) |
||||
|
{ |
||||
|
_attribute = attribute; |
||||
|
} |
||||
|
|
||||
|
protected override IList<object> CreateItemsSource(PropertyItem propertyItem) |
||||
|
{ |
||||
|
var instance = Activator.CreateInstance(_attribute.Type); |
||||
|
return (instance as IItemsSource).GetValues(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue