【问题标题】:Attached Property verboseness附加属性冗长
【发布时间】:2011-11-29 21:40:33
【问题描述】:

我试图了解创建附加属性时发生的一切。

是否需要 SetText()GetText() 方法(通过 sn-p/template 插入并且我在许多示例中看​​到)?框架内的什么在使用它们?

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); }
}

【问题讨论】:

    标签: wpf silverlight dependency-properties


    【解决方案1】:

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

    您不能执行后者,因为您需要以某种方式传递设置属性的实例,因为附加了属性,您没有 this 的实例。

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多