附加属性的详细程度

发布于 2024-12-18 19:08:07 字数 875 浏览 3 评论 0原文

我试图了解当我创建附加属性时发生了什么。

是否需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2024-12-25 19:08:08

它们只是为了您的方便,框架不使用它们。

您不能执行后者,因为您需要以某种方式传递设置了属性的实例,因为属性已附加,您没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文