3

I've one AttachedProperty, which is bound to a singleton.

The attached property:

public static bool? GetIsFocused(DependencyObject obj)
{
    return (bool?)obj.GetValue(IsFocusedProperty);
}

public static void SetIsFocused(DependencyObject obj, bool? value)
{
    obj.SetValue(IsFocusedProperty, value);
}

public static readonly DependencyProperty IsFocusedProperty =
    DependencyProperty.RegisterAttached("IsFocused", typeof(bool?), typeof(FocusExtension), new FrameworkPropertyMetadata(false, OnIsFocusedPropertyChanged) {BindsTwoWayByDefault = true});

private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    FrameworkElement frameworkElement = (FrameworkElement)d;

    if (e.NewValue is bool boolValue && boolValue)
    {
        //Do some stuff
    }
}

And I use it something like this:

<dxe:HyperlinkEdit attachedProperties:FocusExtension.IsFocused="{Binding Source={x:Static configurationFocus:ConfigurationFocusManager.Instance.IsFocus}"
                    Text="SomeText"/>

It works nicely, but one issue I've is that even when the usercontrol is unloaded, I still get my OnIsFocusedPropertyChanged event handler that is triggered.

Is there any way to unregister when the control is disposed or anything similar?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • Did you try [DependencyProperty.UnsetValue](https://social.msdn.microsoft.com/Forums/vstudio/en-US/9d86663b-5fb5-474e-8065-87187f6e35f6/how-to-remove-attached-property-from-controls?forum=wpf) from this question? – Insane Sep 02 '19 at 10:15
  • Clear the binding when `HyperlinkEdit` is unloaded? A control isn't disposable. – mm8 Sep 02 '19 at 13:14

0 Answers0