繁体   English   中英

绑定到不在代码隐藏中的RoutedUICommand

[英]Binding to a RoutedUICommand that's not in the codebehind

我有一个静态类,其中包含我想在绑定中使用的RoutedUICommand。

public static class CommandLibrary
{
    public static ProjectViewModel Project { get; set; }

    public static RoutedUICommand AddPage { get; private set; }

    static CommandLibrary()
    {
        AddPage = new RoutedUICommand("AddPage", "AddPage", typeof(CommandLibrary));

    }

    public static void AddPage_Executed(object sender, ExecutedRoutedEventArgs args)
    {
        Project.AddPage();
    }

    public static void AddPage_CanExecute(object sender, CanExecuteRoutedEventArgs args)
    {
        // We need a project before we can add pages.
        if (Project != null)
        {
            args.CanExecute = true;
        }
        else
        {
            // Did not find project, turning Add Page off.
            args.CanExecute = false;
        }
    }
}

当我尝试为这个AddPage命令创建一个CommandBinding时,VS发出一声发脾气,抱怨说它在Window1中找不到AddPage_CanExecute ...考虑到我看到的所有例子都表明这个XAML应该没问题,这没有任何意义我的代码:

<Window x:Class="MyProject.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyProject">
    <Menu>
        <Menu.CommandBindings>
            <CommandBinding Command="local:CommandLibrary.AddPage" 
                            Executed="AddPage_Executed" CanExecute="AddPage_CanExecute" />
        </Menu.CommandBindings>
        <MenuItem Header="_Page">
            <MenuItem Header="_New" Command="local:CommandLibrary.AddPage" />
        </MenuItem>
    </Menu>
</Window>

我也试过不包括Menu.CommandBindings部分,只是简单地使用它(根据这个问题暗示这一点但不具体):

<MenuItem Header="_New" Command="{x:Static local:CommandLibrary.AddPage}" />

这导致了错误的流程,但它生成的菜单项始终被禁用! CanExecute似乎永远不会被调用。 我假设绑定在这种情况下失败了,尽管更安静。

为什么VS讨厌我的命令并且拒绝在正确的位置查找Executed和CanExecute方法? 我已经看过很多例子(在Matthew McDonald的Pro WPF和在线的几个自定义命令教程中),这些都是我这样做的。

CommandBinding就像可视树中的任何其他元素一样。 在其上指定的任何事件都将由可视树的根处理(在本例中为Window )。 这意味着如果您将AddPage_ExecutedAddPage_CanExecute移动到Window的代码后面,它将起作用。 这允许您在许多UI组件中使用相同的命令,但具有不同的处理程序。

但是,我看到你的命令对你的视图模型执行了一些逻辑。 为了节省您的时间和挫折,请了解路由命令是错误的解决方案。 相反,将您的命令封装在视图模型中,如下所示:

public class ProjectViewModel
{
    private readonly ICollection<PageViewModel> _pages;
    private readonly ICommand _addPageCommand;

    public ProjectViewModel()
    {
        _pages = new ObservableCollection<PageViewModel>();
        _addPageCommand = new DelegateCommand(AddPage);
    }

    public ICommand AddPageCommand
    {
        get { return _addPageCommand; }
    }

    private void AddPage(object state)
    {
        _pages.Add(new PageViewModel());
    }
}

DelegateCommandICommand一个实现,它调用委托来执行和查询命令。 这意味着命令逻辑全部包含在命令中,您不需要CommandBinding来提供处理程序(根本不需要CommandBinding )。 因此,您的视图只是绑定到您的VM,如下所示:

<MenuItem Header="_New" Command="{Binding AddPageCommand}"/>

我建议你仔细阅读这一系列帖子,为你提供更多背景信息:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM