我正在尝试理解创建附加属性时所涉及的所有内容。
在框架中,是否需要使用通过代码段/模板插入的
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); }
}