سؤال

وأريد أن إنشاء DependencyProperty مع 2 خيارات (اليمين واليسار) مشابهة لخصائص مثل LeftAlignment في TextBlock.

لا أحد يعرف رمز المرتبطة بهذا؟ لقد خلق حتى الآن سوى 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); }
}
هل كانت مفيدة؟

المحلول

وببساطة تعيين نوع العقار إلى نوع التعداد بدلا من سلسلة على سبيل المثال:

    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 ) );
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top