2012-12-27 50 views
1

我見過,但是,似乎官方MSDN文檔中關於DependencyProperty.RegisterAttached它不以表示此規定的命名慣例由this question命名約定DependencyProperty.RegisterAttached

暗示我知道代碼必須是這樣的這:

public static readonly DependencyProperty HandleKeyPressEventProperty = 
    DependencyProperty.RegisterAttached("HandleKeyPressEvent", 
             typeof(bool), 
             typeof(MyDataGrid), 
             new UIPropertyMetadata(true)); 
public static bool GetHandleKeyPressEvent(DependencyObject obj) 
{ 
    return (bool)obj.GetValue(HandleKeyPressEventProperty); 
} 
public static void SetHandleKeyPressEvent(DependencyObject obj, bool value) 
{ 
    obj.SetValue(HandleKeyPressEventProperty, value); 
} 

在這種情況下是保持該名稱所需的獲取和設置方法?所需附屬財產以「財產」結束?另外,我可以讓我的代碼,這樣的事情,而不是:

public static readonly DependencyProperty HandleKeyPressEventProperty = 
    DependencyProperty.RegisterAttached("FooEvent", //change registered name 
             typeof(bool), 
             typeof(MyDataGrid), 
             new UIPropertyMetadata(true)); 
public static bool GetHandleKeyPressEvent(DependencyObject obj) 
{ 
    return (bool)obj.GetValue(HandleKeyPressEventProperty); 
} 
public static void SetHandleKeyPressEvent(DependencyObject obj, bool value) 
{ 
    obj.SetValue(HandleKeyPressEventProperty, value); 
} 

誰能收拾這個「神奇」的命名方式,什麼樣的標準我必須遵循?

回答

0

我不確定是否需要那裏的一切,但這也是我使用的格式,我認爲這是我到處看到的。如果你在那裏改變任何東西,即使它仍然有效 - 你會引入混亂和無政府狀態! :)

這是my code snippets一個版本的基礎上,修訂Dr. WPF's snippets

#region IsDirty 
/// <summary> 
/// IsDirty Attached Dependency Property 
/// </summary> 
public static readonly DependencyProperty IsDirtyProperty = 
    DependencyProperty.RegisterAttached(
     "IsDirty", 
     typeof(bool), 
     typeof(AnimationHelper), 
     new PropertyMetadata(false, OnIsDirtyChanged)); 

/// <summary> 
/// Gets the IsDirty property. This dependency property 
/// indicates blah blah blah. 
/// </summary> 
public static bool GetIsDirty(DependencyObject d) 
{ 
    return (bool)d.GetValue(IsDirtyProperty); 
} 

/// <summary> 
/// Sets the IsDirty property. This dependency property 
/// indicates blah blah blah. 
/// </summary> 
public static void SetIsDirty(DependencyObject d, bool value) 
{ 
    d.SetValue(IsDirtyProperty, value); 
} 

/// <summary> 
/// Handles changes to the IsDirty property. 
/// </summary> 
/// <param name="d"> 
/// The <see cref="DependencyObject"/> on which 
/// the property has changed value. 
/// </param> 
/// <param name="e"> 
/// Event data that is issued by any event that 
/// tracks changes to the effective value of this property. 
/// </param> 
private static void OnIsDirtyChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    bool oldIsDirty = (bool)e.OldValue; 
    bool newIsDirty = (bool)d.GetValue(IsDirtyProperty); 
} 
#endregion 
+1

好吧,從我可以告訴,改變像'GetIsDirty'到'GetIsDirtyFoo'將導致附加屬性停止工作...所以,一個神奇的命名方案,並沒有真正的文檔記錄 – Earlz

+0

不知道它是否沒有記錄。 4年前開始學習XAML開發[[WPF Unleashed]](http://www.amazon.com/WPF-4-Unleashed-ebook/dp/B003UBAYXE/ref=sr_1_1_title_1_kin?s=books&ie=UTF8&qid=1356713126&sr= 1-1&keywords = wpf + 4.0 + unleashed)Adam Nathan - 我記得在那裏讀了一些關於各種必需的命名約定的東西,所以它必須記錄在某處。除非它僅記錄爲WPF,而不是用於WinRT/XAML。 –

+0

查看[自定義附加屬性](http://msdn.microsoft.com/en-us/library/windows/apps/hh965327.aspx)文章的確如此:「本主題解釋如何將附加屬性實現爲一個依賴項屬性和**如何定義您的附加屬性在XAML **中可用所必需的訪問器約定。「 –