Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MenuItemAttach. #647

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Build.Props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<Project>
<PropertyGroup>
<UseWPF>true</UseWPF>
<Version>3.5.0.0</Version>
Expand Down
62 changes: 62 additions & 0 deletions src/Shared/HandyControl_Shared/Controls/Attach/MenuItemAttach.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/Shared/HandyControl_Shared/HandyControl_Shared.projitems
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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" />
Expand Down
Loading