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?