WPF设计器自定义属性-下拉列表 [英] WPF designer custom properties - dropdown
问题描述
我想为WPF中的用户控件上的自定义属性提供一个下拉选择. 当我使用Enum作为属性时,一切正常:
I would like to have a drop down selection for a custom property on a User Control in WPF. Everything works fine when I use an Enum as the property:
/// <summary>
/// Interaction logic for Sample.xaml
/// </summary>
public partial class Sample : System.Windows.Controls.UserControl
{
public Sample()
{
InitializeComponent();
}
[DefaultValue(Letters.A)]
[Browsable(true)]
[Category("ControlDisplay")]
[Description("Letter")]
public Letters Letter { get; set; }
public enum Letters
{
A,
B,
C,
D
}
}
太好了:).
但是我想通过自定义类甚至是字符串来实现这一点. 我该怎么办?
But I want to achieve this for a custom class or even a string. How Should I do it?
谢谢.
推荐答案
最后得到了anwser(在挖掘了一些文档之后-很多).
首先是Type Converter
属性
那么此处是一个不错的选择.
Finally got the anwser (after digging some documentation - a lot of it).
First of all there is the Type Converter
atribute
then a nice walktrought how to implement it is here. This is what led me to it.
简而言之:
实现类型转换器GetStandardValuesSupported(ITypeDescriptorContext context)
以返回true,而实现GetStandardValues(ITypeDescriptorContext context)
以返回StandardValuesCollection
作为属性类型.
最后,像这样装饰属性:
In a nutshell:
Implement a type converter GetStandardValuesSupported(ITypeDescriptorContext context)
to return true and GetStandardValues(ITypeDescriptorContext context)
to return the StandardValuesCollection
for the property type.
Finally just decorate the property like so:
[TypeConverter(typeof(MyClassConverter))]
public MyClass MyProperty { get; set; }
designer属性窗口现在将具有一个包含有效值的下拉列表.
The designer property window will now have a dropdown with valid values.
这篇关于WPF设计器自定义属性-下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!