2011-11-29 77 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