附加属性的详细程度
我试图了解当我创建附加属性时发生了什么。
是否需要 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); }
}
I'm trying to understand what all is going on when I create an attached property.
Are the SetText()
and GetText()
methods (that are inserted via the snippet/template and that I see in many examples) required? What within the framework is using them?
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);
}
Could I replace those methods with a simple property, so that I can get/set through a property instead of using those methods?
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们只是为了您的方便,框架不使用它们。
您不能执行后者,因为您需要以某种方式传递设置了属性的实例,因为属性已附加,您没有
this
实例。They are just for your convenience, the framework does not use them.
You cannot do the latter as you need to somehow pass the instance on which the property is set, as the property is attached you do not have the instance as
this
.