Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在上下文菜单wpf中禁用多检查_C#_Wpf_Contextmenu - Fatal编程技术网

C# 在上下文菜单wpf中禁用多检查

C# 在上下文菜单wpf中禁用多检查,c#,wpf,contextmenu,C#,Wpf,Contextmenu,我想禁用上下文菜单项中的多重检查只检查一个项如何处理菜单项的单击事件 private void MenuItem_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem) { IEnumerable<MenuItem> menuItems = null; var mi = (MenuI

我想禁用上下文菜单项中的多重检查只检查一个项如何处理菜单项的单击事件

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;
                    }
            }
        }
private void MenuItem\u单击(对象发送方、路由目标方)
{
如果(发送方为菜单项)
{
IEnumerable menuItems=null;
var mi=(MenuItem)发送方;
如果(mi.Parent是上下文菜单)
menuItems=((ContextMenu)mi.Parent.Items.OfType();
如果(mi.Parent为菜单项)
menuItems=((MenuItem)mi.Parent.Items.OfType();
if(menuItems!=null)
foreach(菜单项中的var项)
{
if(item.ischeckeble&&item!=mi)
item.IsChecked=false;
}
}
}

您可以按照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;
    }
}
公共静态类菜单行为
{
[AttachedPropertyBrowsableForType(typeof(MenuItem))]
公共静态字符串GetOptionGroupName(MenuItem obj)
{
返回(字符串)obj.GetValue(OptionGroupNameProperty);
}
public static void SetOptionGroupName(MenuItem obj,字符串值)
{
obj.SetValue(OptionGroupNameProperty,value);
}
public static readonly dependencProperty OptionGroupNameProperty=
DependencyProperty.RegisterAttached(
“OptionGroupName”,
类型(字符串),
类型(MenuBehavior),
新UIPropertyMetadata(
无效的
选项组名称(已更改);
私有静态void选项GroupNameChanged(DependencyObject o、DependencyPropertyChangedEventArgs e)
{
var menuItem=o作为menuItem;
if(menuItem==null)
返回;
var oldValue=(字符串)e.oldValue;
var newValue=(字符串)e.newValue;
如果(!string.IsNullOrEmpty(oldValue))
{
从OptionGroup(菜单项)中删除;
}
如果(!string.IsNullOrEmpty(newValue))
{
AddToOptionGroup(菜单项);
}
}
专用静态字典GetOptionGroup(DependencyObject obj)
{
return(Dictionary)obj.GetValue(OptionGroupsPropertyKey.DependencyProperty);
}
私有静态void setoptiongroup(DependencyObject对象,字典值)
{
对象设置值(OptionGroupsPropertyKey,值);
}
私有静态只读依赖项PropertyKey选项GroupsPropertyKey=
DependencyProperty.RegisterAttacheDeliverOnly(“OptionGroups”、typeof(字典)、typeof(MenuBehavior)、new-UIPropertyMetadata(null));
私有静态HashSet GetOptionGroup(MenuItem MenuItem,bool create)
{
字符串groupName=GetOptionGroupName(menuItem);
if(groupName==null)
返回null;
如果(menuItem.Parent==null)
返回null;
var optionGroups=GetOptionGroups(menuItem.Parent);
if(optionGroups==null)
{
如果(创建)
{
optionGroups=新字典();
SetOptionGroups(menuItem.Parent,optionGroups);
}
其他的
{
返回null;
}
}
哈希集组;
if(!optionGroups.TryGetValue(组名,组外)&&create)
{
group=新的HashSet();
optionGroups[groupName]=组;
}
返回组;
}
私有静态void AddToOptionGroup(MenuItem MenuItem)
{
var group=GetOptionGroup(menuItem,true);
如果(组==null)
返回;
if(组添加(菜单项))
{
menuItem.Checked+=menuItem\u Checked;
menuItem.Unchecked+=menuItem\u Unchecked;
}
}
private static void RemoveFromOptionGroup(MenuItem MenuItem)
{
var group=GetOptionGroup(menuItem,false);
如果(组==null)
返回;
如果(组删除(菜单项))
{
menuItem.Checked-=menuItem\u Checked;
menuItem.Unchecked-=menuItem\u Unchecked;
}
}
静态无效菜单项已选中(对象发送器、路由EventTarget e)
{
MenuItem MenuItem=发送方作为MenuItem;
if(menuItem==null)
返回;
字符串groupName=GetOptionGroupName(menuItem);
if(groupName==null)
返回;
//允许有多个选中的选项
if(groupName.EndsWith(“*”)| | groupName.EndsWith(“+”))
返回;
var group=GetOptionGroup(menuItem,false);
如果(组==null)
返回;
foreach(组中的var项目)
{
如果(项!=菜单项)
item.IsChecked=false;
}
}
静态无效菜单项未选中(对象发送方、路由EventTarget e)
{
MenuItem MenuItem=发送方作为MenuItem;
if(menuItem==null)
返回;
字符串groupName=GetOptionGroupName(menuItem);
if(groupName==null)
返回;
//允许0选中选项
if(groupName.EndsWith(“*”)| | groupName.EndsWith(“?”)
返回;
var group=GetOptionGroup(menuItem,false);
如果(组==null)
返回;
如果(!group.Any(item=>item.IsChecked))
menuItem.IsChecked=true;
}
}
XAML用法:


my
是一个映射到CLR命名空间的XML命名空间,您在其中声明了
MenuBehavior
类)

当然,还有改进的余地:

  • 您可能希望强制选中一个选项(即impos)