我想在上下文中禁用多项检查菜单项只检查一项如何?
答案 0 :(得分:3)
你可以在代码隐藏中执行它,如DarkSquirrel42的回答中所述。但是,如果您需要可重用的解决方案,最好的方法可能是将其作为附加行为实现,以便您可以直接在XAML中使用它。这是一个基本的实现:
public static class MenuBehavior
{
[AttachedPropertyBrowsableForType(typeof(MenuItem))]
public static string GetOptionGroupName(MenuItem obj)
{
return (string)obj.GetValue(OptionGroupNameProperty);
}
public static void SetOptionGroupName(MenuItem obj, string value)
{
obj.SetValue(OptionGroupNameProperty, value);
}
public static readonly DependencyProperty OptionGroupNameProperty =
DependencyProperty.RegisterAttached(
"OptionGroupName",
typeof(string),
typeof(MenuBehavior),
new UIPropertyMetadata(
null,
OptionGroupNameChanged));
private static void OptionGroupNameChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var menuItem = o as MenuItem;
if (menuItem == null)
return;
var oldValue = (string)e.OldValue;
var newValue = (string)e.NewValue;
if (!string.IsNullOrEmpty(oldValue))
{
RemoveFromOptionGroup(menuItem);
}
if (!string.IsNullOrEmpty(newValue))
{
AddToOptionGroup(menuItem);
}
}
private static Dictionary<string, HashSet<MenuItem>> GetOptionGroups(DependencyObject obj)
{
return (Dictionary<string, HashSet<MenuItem>>)obj.GetValue(OptionGroupsPropertyKey.DependencyProperty);
}
private static void SetOptionGroups(DependencyObject obj, Dictionary<string, HashSet<MenuItem>> value)
{
obj.SetValue(OptionGroupsPropertyKey, value);
}
private static readonly DependencyPropertyKey OptionGroupsPropertyKey =
DependencyProperty.RegisterAttachedReadOnly("OptionGroups", typeof(Dictionary<string, HashSet<MenuItem>>), typeof(MenuBehavior), new UIPropertyMetadata(null));
private static HashSet<MenuItem> GetOptionGroup(MenuItem menuItem, bool create)
{
string groupName = GetOptionGroupName(menuItem);
if (groupName == null)
return null;
if (menuItem.Parent == null)
return null;
var optionGroups = GetOptionGroups(menuItem.Parent);
if (optionGroups == null)
{
if (create)
{
optionGroups = new Dictionary<string, HashSet<MenuItem>>();
SetOptionGroups(menuItem.Parent, optionGroups);
}
else
{
return null;
}
}
HashSet<MenuItem> group;
if (!optionGroups.TryGetValue(groupName, out group) && create)
{
group = new HashSet<MenuItem>();
optionGroups[groupName] = group;
}
return group;
}
private static void AddToOptionGroup(MenuItem menuItem)
{
var group = GetOptionGroup(menuItem, true);
if (group == null)
return;
if (group.Add(menuItem))
{
menuItem.Checked += menuItem_Checked;
menuItem.Unchecked += menuItem_Unchecked;
}
}
private static void RemoveFromOptionGroup(MenuItem menuItem)
{
var group = GetOptionGroup(menuItem, false);
if (group == null)
return;
if (group.Remove(menuItem))
{
menuItem.Checked -= menuItem_Checked;
menuItem.Unchecked -= menuItem_Unchecked;
}
}
static void menuItem_Checked(object sender, RoutedEventArgs e)
{
MenuItem menuItem = sender as MenuItem;
if (menuItem == null)
return;
string groupName = GetOptionGroupName(menuItem);
if (groupName == null)
return;
// More than 1 checked option is allowed
if (groupName.EndsWith("*") || groupName.EndsWith("+"))
return;
var group = GetOptionGroup(menuItem, false);
if (group == null)
return;
foreach (var item in group)
{
if (item != menuItem)
item.IsChecked = false;
}
}
static void menuItem_Unchecked(object sender, RoutedEventArgs e)
{
MenuItem menuItem = sender as MenuItem;
if (menuItem == null)
return;
string groupName = GetOptionGroupName(menuItem);
if (groupName == null)
return;
// 0 checked option is allowed
if (groupName.EndsWith("*") || groupName.EndsWith("?"))
return;
var group = GetOptionGroup(menuItem, false);
if (group == null)
return;
if (!group.Any(item => item.IsChecked))
menuItem.IsChecked = true;
}
}
XAML用法:
<ContextMenu>
<MenuItem Header="Choose one" IsEnabled="False" />
<MenuItem Header="Option 1.1" IsCheckable="True" IsChecked="True"
my:MenuBehavior.OptionGroupName="group1"/>
<MenuItem Header="Option 1.2" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group1"/>
<MenuItem Header="Option 1.3" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group1"/>
<Separator />
<MenuItem Header="Choose zero or one" IsEnabled="False" />
<MenuItem Header="Option 2.1" IsCheckable="True" IsChecked="True"
my:MenuBehavior.OptionGroupName="group2?"/>
<MenuItem Header="Option 2.2" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group2?"/>
<MenuItem Header="Option 2.3" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group2?"/>
<Separator />
<MenuItem Header="Choose one or more" IsEnabled="False" />
<MenuItem Header="Option 3.1" IsCheckable="True" IsChecked="True"
my:MenuBehavior.OptionGroupName="group3+"/>
<MenuItem Header="Option 3.2" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group3+"/>
<MenuItem Header="Option 3.3" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group3+"/>
<Separator />
<MenuItem Header="Choose any number" IsEnabled="False" />
<MenuItem Header="Option 4.1" IsCheckable="True" IsChecked="True"
my:MenuBehavior.OptionGroupName="group4*"/>
<MenuItem Header="Option 4.2" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group4*"/>
<MenuItem Header="Option 4.3" IsCheckable="True"
my:MenuBehavior.OptionGroupName="group4*"/>
</ContextMenu>
(my
是映射到您声明MenuBehavior
类的CLR命名空间的XML命名空间)
当然还有改进的余地:
编辑:我更新了代码以包含上述改进:
MenuItem
?
)+
)*
)。这实际上与完全不使用附加行为相同,但为了完整性,我还是把它包括在内...... XAML用法示例说明了各种规则
答案 1 :(得分:2)
处理MenuItems的Click事件......
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuItem)
{
IEnumerable<MenuItem> menuItems = null;
var mi = (MenuItem)sender;
if (mi.Parent is ContextMenu)
menuItems = ((ContextMenu)mi.Parent).Items.OfType<MenuItem>();
if (mi.Parent is MenuItem)
menuItems = ((MenuItem)mi.Parent).Items.OfType<MenuItem>();
if(menuItems!=null)
foreach (var item in menuItems)
{
if (item.IsCheckable && item != mi)
item.IsChecked = false;
}
}
}