2011-11-29 84 views
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); } 
} 

回答

4

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

你不能做后者,因为你需要以某种方式传递属性设置的实例,因为该属性是附加的,你没有实例this