2012-12-27 168 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 **中可用所必需的访问器约定。“ –