James
James

Reputation: 3635

Custom DependencyProperty

I want to create a DependencyProperty with 2 options (Left and Right) similar to properties like LeftAlignment in a TextBlock.

Does anyone know the code associated with this? I have so far only created simple DependencyPropertys as below:

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

Upvotes: 1

Views: 260

Answers (1)

Andres Denkberg
Andres Denkberg

Reputation: 361

Simply set the type of the property to an enum type instead of string for example:

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

Upvotes: 3

Related Questions