我想创建一个带有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); }
}
答案 0 :(得分:3)
只需将属性的类型设置为枚举类型而不是字符串,例如:
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 ) );