Skip to content

Commit

Permalink
feat: MenuItemGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Sep 27, 2024
1 parent adb7eda commit 1ddad8f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Similar to WPF UI.
MessageBoxResult result = await MessageBox.QuestionAsync("This is a question and do you want to click OK?");
```

- **ToggleButtonGroup** / **RadioButtonGroup**
- **ToggleButtonGroup** / **RadioButtonGroup** / **MenuItemGroup**

> Turn the ToggleButton and RadioButton under the same Group into a radio button.
Expand Down
28 changes: 27 additions & 1 deletion src/Wpf.Ui.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
x:Class="Wpf.Ui.Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="http://schemas.github.com/computedbehaviors/2024/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:Wpf.Ui.Test"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Expand Down Expand Up @@ -572,7 +574,7 @@
Margin="0,8,0,0"
FontSize="14"
FontWeight="Black"
Text="ToggleButtonGroup / RadioButtonGroup" />
Text="ToggleButtonGroup / RadioButtonGroup / MenuItemGroup" />
<StackPanel Margin="0,8,0,0" Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
Expand All @@ -586,6 +588,30 @@
Margin="8,0,0,0"
vio:ToggleButtonGroup.Group="{DynamicResource ToggleButtonGroup}"
Content="2nd" />
<Button Content="MenuItemGroup">
<Button.Resources>
<vio:MenuItemGroup x:Key="MenuItemGroup" IsCanCancel="True" />
</Button.Resources>
<Button.ContextMenu>
<ContextMenu>
<MenuItem
vio:MenuItemGroup.Group="{StaticResource MenuItemGroup}"
Header="Item 1"
IsCheckable="True" />
<MenuItem
vio:MenuItemGroup.Group="{StaticResource MenuItemGroup}"
Header="Item 2"
IsCheckable="True" />
<MenuItem
vio:MenuItemGroup.Group="{StaticResource MenuItemGroup}"
Header="Item 3"
IsCheckable="True" />
</ContextMenu>
</Button.ContextMenu>
<i:Interaction.Behaviors>
<b:LeftContextMenuBehavior />
</i:Interaction.Behaviors>
</Button>
</StackPanel>
<StackPanel Margin="8,0,0,0" Orientation="Horizontal">
<StackPanel.Resources>
Expand Down
3 changes: 3 additions & 0 deletions src/Wpf.Ui.Test/Wpf.Ui.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="WPF-UI" Version="3.0.5" />
<PackageReference Include="ComputedAnimations.WPF" Version="0.4.5-beta" />
<PackageReference Include="ComputedBehaviors.WPF" Version="0.4.5-beta" />
<PackageReference Include="ComputedConverters.WPF" Version="0.4.5-beta" />
</ItemGroup>

<ItemGroup>
Expand Down
76 changes: 76 additions & 0 deletions src/Wpf.Ui.Violeta/Controls/Primitives/MenuItemGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Wpf.Ui.Violeta.Controls.Primitives;

public class MenuItemGroup : List<MenuItem>
{
public bool IsCanCancel { get; set; } = false;

public static MenuItemGroup GetGroup(DependencyObject obj)
{
return (MenuItemGroup)obj.GetValue(GroupProperty);
}

public static void SetGroup(DependencyObject obj, MenuItemGroup value)
{
obj.SetValue(GroupProperty, value);
}

public static readonly DependencyProperty GroupProperty =
DependencyProperty.RegisterAttached("Group", typeof(MenuItemGroup), typeof(MenuItemGroup), new PropertyMetadata(null!, OnGroupChanged));

private static void OnGroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is MenuItem tb)
{
((MenuItemGroup)e.NewValue).Join(tb);
}
}

protected bool Handling { get; set; } = false;

public MenuItemGroup JoinWith(MenuItem menuItem)
{
Join(menuItem);
return this;
}

public void Join(MenuItem menuItem)
{
Add(menuItem);

menuItem.Checked += (s, e) =>
{
if (s is MenuItem mi && GetGroup(mi) is MenuItemGroup group)
{
Handling = true;
foreach (MenuItem tb in group)
{
if (tb != mi)
{
tb.IsChecked = false;
}
}
Handling = false;
}
};
menuItem.Unchecked += (s, e) =>
{
if (!IsCanCancel && !Handling && s is MenuItem mi)
{
mi.IsChecked = true;
}
};
SetGroup(menuItem, this);
}

public void Unjoin(MenuItem checkBox)
{
Remove(checkBox);
SetGroup(checkBox, null!);
}

public static MenuItemGroup New() => [];
}

0 comments on commit 1ddad8f

Please sign in to comment.