0

假设我有以下代码:

   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 标签的引用?

4

1 回答 1

1

e.NewValue?

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

于 2012-05-10T23:39:38.803 回答