DependencyProperties SetValue,GetValue和静态SetPropName()之间的差异

时间:2016-09-06 11:02:54

标签: c# wpf dependency-properties accessor

有两种方法可以访问依赖项属性。行为/反应有什么不同,或者它们的用法相似吗?

第一种方法:

 public static DependencyProperty TestProperty =
        DependencyProperty.RegisterAttached("Test",
                                            typeof (bool),
                                            typeof (MyBaseClass),
                                            new PropertyMetadata(true));

public static bool GetTest(UIElement element)
{
    return (bool) element.GetValue(TestProperty);
}

public static void SetTest(UIElement element, bool value)
{
    element.SetValue(TestProperty, value);
}

第二种方法:

 public static DependencyProperty TestProperty =
        DependencyProperty.Register("Test",
                                            typeof (bool),
                                            typeof (MyBaseClass),
                                            new PropertyMetadata(true));

public bool Test {
    get {
        return (bool) GetValue(TestProperty);
    }
    set {
        SetValue(TestProperty, value);
    }
}

0 个答案:

没有答案