Merge pull request #647 from GF-Huang/MenuItemAttach
Add MenuItemAttach.
This commit is contained in:
3 files changed
+65
-2
No files matched your search
@@ -1,4 +1,4 @@
|
||||
<Project>
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<UseWPF>true</UseWPF>
|
||||
<Version>3.5.0.0</Version>
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using HandyControl.Tools.Extension;
|
||||
|
||||
namespace HandyControl.Controls;
|
||||
|
||||
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)
|
||||
{
|
||||
if (d is not MenuItem menuItem)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
menuItem.Checked -= MenuItem_Checked;
|
||||
menuItem.Click -= MenuItem_Click;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(e.NewValue.ToString()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var groupName = GetGroupName(menuItem);
|
||||
parent
|
||||
.Items
|
||||
.OfType<MenuItem>()
|
||||
.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)
|
||||
{
|
||||
menuItem.IsChecked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||
@@ -23,6 +23,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\InfoElement.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\ListBoxAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\MenuAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\MenuItemAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\MenuTopLineAttach.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PanelElement.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\PasswordBoxAttach.cs" />
|
||||
|
||||
Reference in new issue
Block a user