From 86a5a4924670cee5169709b56c0bb68eb6dbda68 Mon Sep 17 00:00:00 2001 From: GF-Huang Date: Tue, 26 Jan 2021 02:26:30 +0800 Subject: [PATCH 1/2] Add MenuItemAttach. --- .../Controls/Attach/MenuItemAttach.cs | 74 +++++++++++++++++++ .../HandyControl_Shared.projitems | 1 + 2 files changed, 75 insertions(+) create mode 100644 src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs new file mode 100644 index 000000000..ea482ed67 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs @@ -0,0 +1,74 @@ +using System.Linq; +using System.Windows; +using System.Windows.Controls; + +namespace HandyControl.Controls +{ + public class MenuItemAttach + { + public static readonly DependencyProperty GroupNameProperty = + DependencyProperty.RegisterAttached("GroupName", typeof(string), typeof(MenuItemAttach), + new PropertyMetadata(string.Empty, GroupNamePropertyChanged)); + + [AttachedPropertyBrowsableForType(typeof(MenuItem))] + public static string GetGroupName(DependencyObject obj) + { + return (string) obj.GetValue(GroupNameProperty); + } + + [AttachedPropertyBrowsableForType(typeof(MenuItem))] + public static void SetGroupName(DependencyObject obj, string value) + { + obj.SetValue(GroupNameProperty, value); + } + + private static void GroupNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is MenuItem menuItem) + { + var newGroupName = e.NewValue.ToString(); + var oldGroupName = e.OldValue.ToString(); + + if (string.IsNullOrWhiteSpace(newGroupName)) + { + menuItem.Checked -= MenuItem_Checked; + menuItem.Click -= MenuItem_Click; + + } + else if (string.IsNullOrWhiteSpace(oldGroupName)) + { + // When the oldGroupName is null or white space, + // it means we had removed the Checked event handler or never add, so we need add one. + menuItem.Checked += MenuItem_Checked; + + // The same to Checked event. + menuItem.Click += MenuItem_Click; + } + } + } + + private static void MenuItem_Checked(object sender, RoutedEventArgs e) + { + if (sender is MenuItem menuItem && menuItem.Parent is MenuItem parent) + { + var groupName = GetGroupName(menuItem); + + foreach (var item in parent.Items.OfType().Where(m => m != menuItem && + m.IsCheckable && + GetGroupName(m) == groupName)) + { + item.IsChecked = false; + } + } + } + + private static void MenuItem_Click(object sender, RoutedEventArgs e) + { + // prevent uncheck when click the checked menu item + if (e.OriginalSource is MenuItem menuItem && !menuItem.IsChecked) + { + menuItem.IsChecked = true; + } + } + } +} diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index d603f1911..98c31e70f 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -19,6 +19,7 @@ + From e84e2b1ab1f1378bf976aff10ad9a3d847c87e6c Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Thu, 28 Sep 2023 21:11:50 +0800 Subject: [PATCH 2/2] chore: format code. --- src/Directory.Build.Props | 4 +- .../Controls/Attach/MenuItemAttach.cs | 96 ++++++++----------- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/src/Directory.Build.Props b/src/Directory.Build.Props index 18714da4a..3cf3de12a 100644 --- a/src/Directory.Build.Props +++ b/src/Directory.Build.Props @@ -4,8 +4,8 @@ 3.0.0.0 3.0.0.0 3.0.0.0 - 8.0 + latest Copyright © HandyOrg 2018-2020 HandyOrg - \ No newline at end of file + diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs index ea482ed67..1ae818468 100644 --- a/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs +++ b/src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs @@ -1,74 +1,62 @@ using System.Linq; using System.Windows; using System.Windows.Controls; +using HandyControl.Tools.Extension; -namespace HandyControl.Controls +namespace HandyControl.Controls; + +public class MenuItemAttach { - public class MenuItemAttach - { - public static readonly DependencyProperty GroupNameProperty = - DependencyProperty.RegisterAttached("GroupName", typeof(string), typeof(MenuItemAttach), - new PropertyMetadata(string.Empty, GroupNamePropertyChanged)); + public static readonly DependencyProperty GroupNameProperty = + DependencyProperty.RegisterAttached("GroupName", typeof(string), typeof(MenuItemAttach), + new PropertyMetadata(string.Empty, OnGroupNameChanged)); - [AttachedPropertyBrowsableForType(typeof(MenuItem))] - public static string GetGroupName(DependencyObject obj) - { - return (string) obj.GetValue(GroupNameProperty); - } + [AttachedPropertyBrowsableForType(typeof(MenuItem))] + public static string GetGroupName(DependencyObject obj) => (string) obj.GetValue(GroupNameProperty); - [AttachedPropertyBrowsableForType(typeof(MenuItem))] - public static void SetGroupName(DependencyObject obj, string value) - { - obj.SetValue(GroupNameProperty, value); - } + [AttachedPropertyBrowsableForType(typeof(MenuItem))] + public static void SetGroupName(DependencyObject obj, string value) => obj.SetValue(GroupNameProperty, value); - private static void GroupNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is not MenuItem menuItem) { - if (d is MenuItem menuItem) - { - var newGroupName = e.NewValue.ToString(); - var oldGroupName = e.OldValue.ToString(); - - if (string.IsNullOrWhiteSpace(newGroupName)) - { - menuItem.Checked -= MenuItem_Checked; - menuItem.Click -= MenuItem_Click; + return; + } - } - else if (string.IsNullOrWhiteSpace(oldGroupName)) - { - // When the oldGroupName is null or white space, - // it means we had removed the Checked event handler or never add, so we need add one. - menuItem.Checked += MenuItem_Checked; + menuItem.Checked -= MenuItem_Checked; + menuItem.Click -= MenuItem_Click; - // The same to Checked event. - menuItem.Click += MenuItem_Click; - } - } + if (string.IsNullOrWhiteSpace(e.NewValue.ToString())) + { + return; } - private static void MenuItem_Checked(object sender, RoutedEventArgs e) - { - if (sender is MenuItem menuItem && menuItem.Parent is MenuItem parent) - { - var groupName = GetGroupName(menuItem); + menuItem.Checked += MenuItem_Checked; + menuItem.Click += MenuItem_Click; + } - foreach (var item in parent.Items.OfType().Where(m => m != menuItem && - m.IsCheckable && - GetGroupName(m) == groupName)) - { - item.IsChecked = false; - } - } + private static void MenuItem_Checked(object sender, RoutedEventArgs e) + { + if (sender is not MenuItem { Parent: MenuItem parent } menuItem) + { + return; } - private static void MenuItem_Click(object sender, RoutedEventArgs e) + var groupName = GetGroupName(menuItem); + parent + .Items + .OfType() + .Where(item => item != menuItem && item.IsCheckable && string.Equals(GetGroupName(item), groupName)) + .Do(item => item.IsChecked = false); + } + + private static void MenuItem_Click(object sender, RoutedEventArgs e) + { + // prevent uncheck when click the checked menu item + if (e.OriginalSource is MenuItem { IsChecked: false } menuItem) { - // prevent uncheck when click the checked menu item - if (e.OriginalSource is MenuItem menuItem && !menuItem.IsChecked) - { - menuItem.IsChecked = true; - } + menuItem.IsChecked = true; } } }