I have created a custom page header for my project in which I have set up a DependencyProperty for CloseButtonCommandProperty. I have added a callback to this Property to set another DependencyProperty (ShowCloseButtonProperty).
The issue I am having is that the null is not being hit, so ShowCloseButton is always set to true.
I am a bit green on dependency properties, so any help (or corrections on my code) are very welcome.
public static readonly DependencyProperty CloseButtonCommandProperty =
DependencyProperty.Register(nameof(CloseButtonCommand), typeof(AsyncRelayCommand), typeof(WindowHeader),
new PropertyMetadata(null!, new PropertyChangedCallback(OnCloseButtonCommandChanged)));
public AsyncRelayCommand CloseButtonCommand
{
get => (AsyncRelayCommand)GetValue(CloseButtonCommandProperty);
set => SetValue(CloseButtonCommandProperty, value);
}
private static void OnCloseButtonCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WindowHeader ctrl)
ctrl.ShowCloseButton = e.NewValue != null;
}
public static readonly DependencyProperty ShowCloseButtonProperty =
DependencyProperty.Register(nameof(ShowCloseButton), typeof(bool), typeof(WindowHeader),
new PropertyMetadata(false));
public bool ShowCloseButton
{
get => (bool)GetValue(ShowCloseButtonProperty);
private set => SetValue(ShowCloseButtonProperty, value);
}