WPF designer custom properties - dropdown

我怕爱的太早我们不能终老 提交于 2019-12-11 16:43:59

问题


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
    }
}

Awesome :).

But I want to achieve this for a custom class or even a string. How Should I do it?

Thanks in advance.


回答1:


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.

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; }

The designer property window will now have a dropdown with valid values.



来源:https://stackoverflow.com/questions/45770017/wpf-designer-custom-properties-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!