附加属性的冗长性

3
我正在尝试理解创建附加属性时所涉及的所有内容。 在框架中,是否需要使用通过代码段/模板插入的SetText()GetText()方法(我在许多示例中看到它们)?有哪些部分在框架中使用它们?
public static readonly DependencyProperty TextProperty =
    DependencyProperty.RegisterAttached("Text",
                                        typeof(string),
                                        typeof(FundIndexDataHeaderItem),
                                        new PropertyMetadata(default(string)));

public static void SetText(UIElement element, string value)
{
    element.SetValue(TextProperty, value);
}

public static string GetText(UIElement element)
{
    return (string)element.GetValue(TextProperty);
}

我能否用一个简单的属性来替换那些方法,这样我就可以通过属性来获取/设置,而不是使用那些方法吗?

public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}
1个回答

4

这些只是为了方便而存在,框架并不使用它们。

由于需要传递设置属性的实例,所以您无法执行后者,因为该属性是附加的,您没有实例作为 this


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接