WPF:添加命令以通过绑定菜单项自动生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14098810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:50:02  来源:igfitidea点击:

WPF: Add a command to auto-generated by binding menu items

wpfmvvmbindingcommand

提问by trickbz

MVVM is used. I created separate menu 'Recent files' which gets its items from binding. It looks like that:

使用 MVVM。我创建了单独的菜单“最近的文件”,它从绑定中获取项目。它看起来像这样:

enter image description here

在此处输入图片说明

        <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
        </MenuItem>

Now, I would like to add Command to each of the those auto-generated items, which should get the path as command parameter and execute import file action by click.

现在,我想将 Command 添加到每个自动生成的项目中,这些项目应该获取路径作为命令参数并通过单击执行导入文件操作。

Could you please suggest how can it be done in MVVM way?

您能否建议如何以 MVVM 方式完成?

回答by trickbz

Again, found the solution by myself. I tried to put the command in wrong way like below, and it doesn't work:

再次,自己找到了解决方案。我试图以错误的方式放置命令,如下所示,但它不起作用:

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding ImportRecentItemCommand}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

Here is the right approach. Still don't understand how it works, have to learn WPF deeply!

这是正确的方法。还是不明白怎么操作,还得深入学习WPF!

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

EDIT: The final version

编辑:最终版本

XAML:

XAML:

            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                    <Setter Property="CommandParameter" Value="{Binding}" />
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>

ViewModel: MVVM Light Toolkit is used, RelayCommand goes from there:

ViewModel:使用了 MVVM Light Toolkit,RelayCommand 从那里开始:

        private ICommand _importRecentItemCommand;

        public ICommand ImportRecentItemCommand
        {
            get { return _importRecentItemCommand ?? (_importRecentItemCommand = new RelayCommand<object>(ImportRecentItemCommandExecuted)); }
        }

        private void ImportRecentItemCommandExecuted(object parameter)
        {
            MessageBox.Show(parameter.ToString());
        }

Enjoy

享受