Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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# e、 CanExecute=false,使上下文菜单不可见而不是禁用_C#_Wpf_Xaml - Fatal编程技术网

C# e、 CanExecute=false,使上下文菜单不可见而不是禁用

C# e、 CanExecute=false,使上下文菜单不可见而不是禁用,c#,wpf,xaml,C#,Wpf,Xaml,在Windows 7中,e.CanExecute=false正在禁用contextmenu项。但在Windows Server2008R2中,它不起作用,即菜单项不可见(消失)。为什么这在Windows Server 2008R2中不起作用?还有其他的吗 -----编辑---------- 以下是代码,它在Windows7中运行良好,但在WindowsServer2008R2中运行不正常 下面是代码片段: XAML代码: <ContextMenu x:Key="SharedContextM

在Windows 7中,e.CanExecute=false正在禁用contextmenu项。但在Windows Server2008R2中,它不起作用,即菜单项不可见(消失)。为什么这在Windows Server 2008R2中不起作用?还有其他的吗

-----编辑----------

以下是代码,它在Windows7中运行良好,但在WindowsServer2008R2中运行不正常 下面是代码片段:

XAML代码:

<ContextMenu x:Key="SharedContextMenu">
    <ContextMenu.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Cut" CanExecute="EnableContextMenu" Executed="ContextMenuCommandHandled" />
        <CommandBinding Command="ApplicationCommands.Copy" CanExecute="EnableContextMenu" Executed="ContextMenuCommandHandled" />
        <CommandBinding Command="ApplicationCommands.Paste" CanExecute="EnableContextMenu" Executed="ContextMenuCommandHandled" />
    </ContextMenu.CommandBindings>
    <MenuItem Command ="Cut" InputGestureText=" "/>
    <MenuItem Command ="Copy" InputGestureText=" "/>
    <MenuItem Command= "Paste" InputGestureText=" "/>
</ContextMenu>

你能把代码贴在你设置e.CanExecute=false的地方吗。我猜这是一个路由命令。你不能发布这样的问题。这对任何人都没有意义。我怀疑Windows 2008中应用于ContextMenu的样式与Windows 7中应用的样式不同。可能隐藏了菜单项,如问题所示:是的,Win7和Win2008中的上下文菜单样式也不同。这可能就是这个问题的原因。现在如何解决这个问题呢?
private void EnableContextMenu(object sender, CanExecuteRoutedEventArgs e)
{
    try
    {
        ContextMenu controlContextMenu = sender as ContextMenu;
        if (controlContextMenu != null && controlContextMenu.PlacementTarget != null)
        {
            e.CanExecute = true;
            if (controlContextMenu.PlacementTarget is ComboBox)
            {
                if (string.IsNullOrEmpty(((ComboBox)controlContextMenu.PlacementTarget).Text))
                {
                    e.CanExecute = false;
                }
            }
        }
    }
    catch (Exception exceptionInstance)
    {
        LogManager.LogException("EnableContextMenu(): " + exceptionInstance.Message);
        MessageBox.Show(exceptionInstance.Message, Constants.SCREEN_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
    }
}