Thursday, May 5, 2011

Custom DependancyProperty

Hi,

I want to create a DependancyProperty 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 onl created simple DependancyProperty's 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); }
}
From stackoverflow
  • 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 ) );
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.