WPF 上下文菜单命令绑定已禁用

发布于 2024-12-10 03:07:57 字数 1987 浏览 2 评论 0原文

我有一个按钮和一个上下文菜单绑定到同一个命令,当我启动应用程序时,按钮被启用,但上下文菜单项没有(正如我所说,它们绑定到同一个命令)。仅当我单击按钮后,上下文菜单项才会启用。有人知道为什么吗?

这就是行为。

这是 XAML:

<Window.CommandBindings>
    <CommandBinding Command="local:LocalCommandManager.ShowDialogCommand" CanExecute="CanExecuteShowDialogCommand" Executed="ShowDialogCommandExecuted" />
    <CommandBinding Command="local:LocalCommandManager.DontShowDialogCommand" CanExecute="CanExecuteDontShowDialogCommand" Executed="DontShowDialogCommandExecuted" />
</Window.CommandBindings>
<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Command="local:LocalCommandManager.ShowDialogCommand" />
        <MenuItem Command="local:LocalCommandManager.DontShowDialogCommand" />
    </ContextMenu>
</Window.ContextMenu>
<Grid Background="Red">
    <Button Command="local:LocalCommandManager.ShowDialogCommand" Content="Show Dialog" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="6" />
</Grid>

谢谢!

编辑:

命令代码:

public static class LocalCommandManager
{
    private static object syncRoot = new object();

    private static RoutedUICommand _showDialogCommand;
    public static RoutedUICommand ShowDialogCommand
    {
        get
        {
            lock (syncRoot)
            {
                if (_showDialogCommand == null)
                    _showDialogCommand = new RoutedUICommand("Show Dialog", "ShowDialogCommand", typeof(LocalCommandManager));
                return _showDialogCommand;
            }
        }
    }
}

命令事件处理程序(在 MainWindow.xaml.cs 中):

private void CanExecuteShowDialogCommand(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

private void ShowDialogCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Dialog");
}

I have a button and a context menu bound to the same Command, when I start the app, the button is enabled, but the context menu item isn't (as I said, they're bound to the same command). The context menu item becomes enabled only after I click the button. Does anybody know why?

This is the behavior.

And this is the XAML:

<Window.CommandBindings>
    <CommandBinding Command="local:LocalCommandManager.ShowDialogCommand" CanExecute="CanExecuteShowDialogCommand" Executed="ShowDialogCommandExecuted" />
    <CommandBinding Command="local:LocalCommandManager.DontShowDialogCommand" CanExecute="CanExecuteDontShowDialogCommand" Executed="DontShowDialogCommandExecuted" />
</Window.CommandBindings>
<Window.ContextMenu>
    <ContextMenu>
        <MenuItem Command="local:LocalCommandManager.ShowDialogCommand" />
        <MenuItem Command="local:LocalCommandManager.DontShowDialogCommand" />
    </ContextMenu>
</Window.ContextMenu>
<Grid Background="Red">
    <Button Command="local:LocalCommandManager.ShowDialogCommand" Content="Show Dialog" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="6" />
</Grid>

Thanks!

EDIT:

Command code:

public static class LocalCommandManager
{
    private static object syncRoot = new object();

    private static RoutedUICommand _showDialogCommand;
    public static RoutedUICommand ShowDialogCommand
    {
        get
        {
            lock (syncRoot)
            {
                if (_showDialogCommand == null)
                    _showDialogCommand = new RoutedUICommand("Show Dialog", "ShowDialogCommand", typeof(LocalCommandManager));
                return _showDialogCommand;
            }
        }
    }
}

Command event handlers (in MainWindow.xaml.cs):

private void CanExecuteShowDialogCommand(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

private void ShowDialogCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Dialog");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文