在OnChange事件中找到附加属性附加到的控件

时间:2012-05-10 23:37:00

标签: c# .net wpf binding

说我有以下代码:

   public static DependencyProperty LabelProperty =
        DependencyProperty.RegisterAttached(
            "Label",
            typeof(Label),
            typeof(HotKeyHelper),
            new FrameworkPropertyMetadata(default(Label), OnLabelChanged)
        );

    public static void SetLabel(DependencyObject obj, Label value)
    {
        obj.SetValue(LabelProperty, value);
    }

    public static Label GetLabel(DependencyObject obj)
    {
        return (Label)obj.GetValue(LabelProperty);
    }

    private static void OnLabelChanged(DependencyObject obj,
         DependencyPropertyChangedEventArgs e)
    {
        Label label = obj as Label;

        // Question is for Right Here!
    }

OnLabelChanged事件中是否有办法获取附加到的对象?

例如,假设我像这样使用它:

  <TextBox Controls:HotKeyHelper.Label="{Binding ElementName=SomeLabel}"/>

有没有办法在我的OnLabelChanged事件中获取对SomeLabel标签的引用?

1 个答案:

答案 0 :(得分:1)

e.NewValue

obj应该是附加属性设置的对象,我怀疑你是否想将它转换为Label,因为它可以是任何东西......