Skip to content

Commit

Permalink
MenuItem and HyperLink support added in OnNotifyCommandParameterChang…
Browse files Browse the repository at this point in the history
…esChanged
  • Loading branch information
sonnemaf committed Dec 8, 2020
1 parent 6ca6906 commit 0a1c5d4
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions MvvmDemoWpf/Views/Windows/MvvmHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;

namespace MvvmDemoWPF.Views.Windows {
Expand All @@ -26,28 +28,60 @@ public class MvvmHelper {
/// <param name="d">FrameworkElement that changed its NotifyCommandParameterChanges attached property.</param>
/// <param name="e">DependencyPropertyChangedEventArgs with the new and old value.</param>
private static void OnNotifyCommandParameterChangesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
if (d is ICommandSource source) {
var value = (bool)e.NewValue;
if (source is ButtonBase btn) {
var pd = DependencyPropertyDescriptor.FromProperty(ButtonBase.CommandParameterProperty, typeof(ButtonBase));

RoutedEventHandler unloaded = (s, e) => {
pd.RemoveValueChanged(btn, OnCommandParameterChanged);
};

if (value) {
pd.AddValueChanged(btn, OnCommandParameterChanged);
btn.Unloaded += unloaded;
} else {
pd.RemoveValueChanged(btn, OnCommandParameterChanged);
btn.Unloaded -= unloaded;
}
if (d is FrameworkElement frameworkElement) {
DependencyPropertyDescriptor pd;

switch (frameworkElement) {
case ButtonBase _:
pd = DependencyPropertyDescriptor.FromProperty(ButtonBase.CommandParameterProperty, typeof(ButtonBase));
break;
case MenuItem _:
pd = DependencyPropertyDescriptor.FromProperty(MenuItem.CommandParameterProperty, typeof(MenuItem));
break;
default:
return;
}

RoutedEventHandler unloaded = (s, e) => {
pd.RemoveValueChanged(frameworkElement, OnCommandParameterChanged);
};

if ((bool)e.NewValue) {
pd.AddValueChanged(frameworkElement, OnCommandParameterChanged);
frameworkElement.Unloaded += unloaded;
} else {
pd.RemoveValueChanged(frameworkElement, OnCommandParameterChanged);
frameworkElement.Unloaded -= unloaded;
}

} else if (d is FrameworkContentElement frameworkContentElement) {
DependencyPropertyDescriptor pd;

switch (frameworkContentElement) {
case Hyperlink hl:
pd = DependencyPropertyDescriptor.FromProperty(Hyperlink.CommandParameterProperty, typeof(Hyperlink));
break;
default:
return;
}

RoutedEventHandler unloaded = (s, e) => {
pd.RemoveValueChanged(frameworkContentElement, OnCommandParameterChanged);
};

if ((bool)e.NewValue) {
pd.AddValueChanged(frameworkContentElement, OnCommandParameterChanged);
frameworkContentElement.Unloaded += unloaded;
} else {
pd.RemoveValueChanged(frameworkContentElement, OnCommandParameterChanged);
frameworkContentElement.Unloaded -= unloaded;
}

}
}

private static void OnCommandParameterChanged(object sender, EventArgs e) {
((sender as ButtonBase).Command as IRelayCommand)?.NotifyCanExecuteChanged();
((sender as ICommandSource).Command as IRelayCommand)?.NotifyCanExecuteChanged();
}

/// <summary>
Expand Down

0 comments on commit 0a1c5d4

Please sign in to comment.