1

我已经看过关于 的官方MSDN文档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);
}

在这种情况下,是否需要 Get 和 Set 方法来保留该名称?附加财产是否必须以“财产”结尾?另外,我可以让我的代码变成这样:

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

谁能弄清楚这个“神奇”的命名方案以及我必须遵循什么样的标准?

4

1 回答 1

0

我不确定是否需要其中的所有内容,但这也是我使用的格式,我认为这是我随处可见的格式。如果您更改其中的任何内容,即使它仍然有效 - 您将引入混乱和无政府状态!:)

这是我的代码片段中的一个版本,基于修订后的 Dr. WPF 的片段

#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
于 2012-12-28T09:57:08.017 回答