将项目绑定到 MenuItem ->使用命令

Bind Items to MenuItem -gt; use Command(将项目绑定到 MenuItem -使用命令)
本文介绍了将项目绑定到 MenuItem ->使用命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 MenuItem,其中包含一组项目.它看起来像文件 -> 打开菜单项.

I have a MenuItem, which has a collection of items in it. It looks like the File -> Open Menuitem.

所以:

  • 文件
    • 打开
      • 从数据库打开
        • 文件 1
        • 文件 2
        • 文件 3

        XAML 代码:

        <Menu>
        <MenuItem Header="File">
            <MenuItem Header="Open">
                <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}"/>
            </MenuItem>
        </MenuItem>
        </Menu>
        

        我想在单击特定项目时调用命令.示例:用户单击文件 1,应调用命令,其中文件 1"是命令参数.

        I want to call a Command, when a specific item has been clicked. Example: User clicks on File 1, a command should be called where the "File 1" is the Command Parameter.

        ViewModel 包含我想在 MenuItem集合"中显示的项目

        ViewModel contains the Items, which I want to display in the MenuItem "collection"

        private ObservableCollection<string> _OCFragebogen;
        public ObservableCollection<string> OCFragebogen
        {
            get
            {
                if (_OCFragebogen == null)
                    _OCFragebogen = new ObservableCollection<string>();
                return _OCFragebogen;
            }
            set
            {
                _OCFragebogen = value;
                RaisePropertyChanged(() => OCFragebogen);
            }
        }
        

        明确地说:当用户点击 MenuItem 中的一个项目(来自 ItemsSource)时,应该调用一个命令,我想对点击的项目执行某些操作.

        To make it clear: When the user clicks on an item (from the ItemsSource) in the MenuItem, a Command should be called where I want to do something with the clicked Item.

        我必须在哪里使用命令来调用 ViewModel 中的方法 (RelayCommand)?我希望在单击 ItemsSource 中的一个项目时使用它 + 我想将单击的项目传递给该方法.

        Where do I have to use the command to call a method (RelayCommand) in my ViewModel? I want it to be used when an Item from the ItemsSource has been clicked + I want to pass the clicked item to the method.

        推荐答案

        这应该对你有用

        <MenuItem Header="From Database" 
                  ItemsSource="{Binding YourItemSource}">
            <MenuItem.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.YourCommandName}"></Setter>
                    <Setter Property="CommandParameter" Value="{Binding}"></Setter>
                 </Style>
             </MenuItem.ItemContainerStyle>
        </MenuItem>
        

        这篇关于将项目绑定到 MenuItem ->使用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

        本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

ActiveDirectory error 0x8000500c when traversing properties(遍历属性时 ActiveDirectory 错误 0x8000500c)
search by samaccountname with wildcards(使用通配符按 samaccountname 搜索)
Get the list of Groups for the given UserPrincipal(获取给定 UserPrincipal 的组列表)
Can you find an Active Directory User#39;s Primary Group in C#?(你能在 C# 中找到 Active Directory 用户的主要组吗?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)