简体   繁体   中英

Find the control that an Attached Property is attached to in the OnChange Event

Say I have the following Code:

   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!
    }

Is there a way in the OnLabelChanged event to get the object it is attached to?

For example, say I use this like this:

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

Is there a way to get a reference to the SomeLabel label in my OnLabelChanged event?

e.NewValue ?

obj should be the object the attached property is set on, i doubt that you'll want to cast it to Label as it can be anything...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM