【问题标题】:Custom DependencyProperty自定义依赖属性
【发布时间】:2009-05-05 09:16:18
【问题描述】:

我想创建一个具有 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); }
}

【问题讨论】:

    标签: c# wpf dependency-properties


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2012-05-30
      • 2012-08-29
      • 1970-01-01
      • 2011-09-09
      • 2012-08-07
      • 1970-01-01
      • 2013-06-20
      相关资源
      最近更新 更多