我想创建一个带有2个选项(左和右)的DependencyProperty,类似于TextBlock中的LeftAlignment等属性。

有谁知道与此相关的代码?到目前为止,我只创建了简单的DependencyPropertys,如下所示:

public static readonly DependencyProperty AlignProperty = DependencyProperty.Register("Align", typeof(string), typeof(HalfCurvedRectangle), new FrameworkPropertyMetadata("Left", FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));

[TypeConverter(typeof(StringConverter))]
public string Align
{
     get { return (string)base.GetValue(AlignProperty); }
     set { base.SetValue(AlignProperty, value); }
}
有帮助吗?

解决方案

只需将属性的类型设置为枚举类型而不是字符串,例如:

    public enum BrushTypes
    {
        Solid,
        Gradient
    }

    public BrushTypes BrushType
    {
        get { return ( BrushTypes )GetValue( BrushTypeProperty ); }
        set { SetValue( BrushTypeProperty, value ); }
    }

    public static readonly DependencyProperty BrushTypeProperty = 
               DependencyProperty.Register( "BrushType", 
                                            typeof( BrushTypes ), 
                                            typeof( MyClass ) );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top