chore: format code.
This commit is contained in:
1 parent
86a5a49246
commit
e84e2b1ab1
2 files changed
+44
-56
No files matched your search
@@ -4,8 +4,8 @@
|
|||||||
<Version>3.0.0.0</Version>
|
<Version>3.0.0.0</Version>
|
||||||
<FileVersion>3.0.0.0</FileVersion>
|
<FileVersion>3.0.0.0</FileVersion>
|
||||||
<AssemblyVersion>3.0.0.0</AssemblyVersion>
|
<AssemblyVersion>3.0.0.0</AssemblyVersion>
|
||||||
<LangVersion>8.0</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<Copyright>Copyright © HandyOrg 2018-2020</Copyright>
|
<Copyright>Copyright © HandyOrg 2018-2020</Copyright>
|
||||||
<Authors>HandyOrg</Authors>
|
<Authors>HandyOrg</Authors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,74 +1,62 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
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, OnGroupNameChanged));
|
||||||
|
|
||||||
|
[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);
|
||||||
|
|
||||||
|
private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
public static readonly DependencyProperty GroupNameProperty =
|
if (d is not MenuItem menuItem)
|
||||||
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);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
[AttachedPropertyBrowsableForType(typeof(MenuItem))]
|
menuItem.Checked -= MenuItem_Checked;
|
||||||
public static void SetGroupName(DependencyObject obj, string value)
|
menuItem.Click -= MenuItem_Click;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(e.NewValue.ToString()))
|
||||||
{
|
{
|
||||||
obj.SetValue(GroupNameProperty, value);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void GroupNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
menuItem.Checked += MenuItem_Checked;
|
||||||
|
menuItem.Click += MenuItem_Click;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void MenuItem_Checked(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (sender is not MenuItem { Parent: MenuItem parent } menuItem)
|
||||||
{
|
{
|
||||||
if (d is MenuItem menuItem)
|
return;
|
||||||
{
|
|
||||||
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)
|
var groupName = GetGroupName(menuItem);
|
||||||
{
|
parent
|
||||||
if (sender is MenuItem menuItem && menuItem.Parent is MenuItem parent)
|
.Items
|
||||||
{
|
.OfType<MenuItem>()
|
||||||
var groupName = GetGroupName(menuItem);
|
.Where(item => item != menuItem && item.IsCheckable && string.Equals(GetGroupName(item), groupName))
|
||||||
|
.Do(item => item.IsChecked = false);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var item in parent.Items.OfType<MenuItem>().Where(m => m != menuItem &&
|
private static void MenuItem_Click(object sender, RoutedEventArgs e)
|
||||||
m.IsCheckable &&
|
{
|
||||||
GetGroupName(m) == groupName))
|
// prevent uncheck when click the checked menu item
|
||||||
{
|
if (e.OriginalSource is MenuItem { IsChecked: false } menuItem)
|
||||||
item.IsChecked = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void MenuItem_Click(object sender, RoutedEventArgs e)
|
|
||||||
{
|
{
|
||||||
// prevent uncheck when click the checked menu item
|
menuItem.IsChecked = true;
|
||||||
if (e.OriginalSource is MenuItem menuItem && !menuItem.IsChecked)
|
|
||||||
{
|
|
||||||
menuItem.IsChecked = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user