2009-05-05 73 views
1

我想創建一個DependencyProperty,其中包含兩個選項(左和右),類似於TextBlock中的LeftAlignment屬性。Custom DependencyProperty

有誰知道與此相關的代碼?我迄今只創造瞭如下簡單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); } 
} 

回答

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