none
MVVM模式下,如何在ViewModel对象中获取点击xaml中已经设置好的MenuItem的header属性? RRS feed

  • 问题

  •   <MenuItem Header="设置"  >
                                        <MenuItem Header="1" Command="{binding selectItem}" ></MenuItem>
                                        <MenuItem Header="2" Command="{binding selectItem}"></MenuItem>
                                        <MenuItem Header="3" Command="{binding selectItem}"></MenuItem>
                       
                                    </MenuItem>

    header属性为“设置”的menuitem里,有三个子MenuItem都绑定了同一个命令,没有进行过数据绑定,都是在xaml设置好了的。如何在这个命令的匿名方法中获取被点击的MenuItem属性?(MenuIten以后会增加)

    命令代码

     class DelegateCommand : ICommand
        {
            public bool CanExecute(object parameter)
            {
                if (this.CanExecuteFunc == null)
                {
                    return true;
                }
    
                return this.CanExecuteFunc(parameter);
            }
    
            public event EventHandler CanExecuteChanged;
    
            public void Execute(object parameter)
            {
                if (this.ExecuteAction == null)
                {
                    return;
                }
                this.ExecuteAction(parameter);
            }
    
            public Action<object> ExecuteAction { get; set; }
    
            public Func<object, bool> CanExecuteFunc { get; set; }
        }



    2017年1月25日 6:32

答案


  • Hi 轮回的齿轮,

    >>MVVM模式下,如何在ViewModel对象中获取点击xaml中已经设置好的MenuItem的header属性?

    平常我们可以这样获取:

        <Window.CommandBindings>
            <CommandBinding Command="New" CanExecute="NewCommand_CanExecute" Executed="NewCommand_Executed" />
           
        </Window.CommandBindings>
    
          <Menu DockPanel.Dock="Top">
                <MenuItem Header="_File">
                    <MenuItem Command="New" />
                    <Separator />
                    <MenuItem Header="_Exit" />
                    <Separator />
                    <MenuItem Header="1231"  Command="{Binding RemoveCommand}" CommandParameter="{Binding XPath= name }" />
                </MenuItem>
            </Menu>
    
            private void NewCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }
    
            private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                var MI= (MenuItem)LogicalTreeHelper.GetParent(e.Source as DependencyObject);
                txtEditor.Text = ""+ MI.Header.ToString();
            }

    MVVM模式下: 我建议你通过绑定对应参数来确定是哪个菜单出发了事件

    <Menu ItemsSource="{Binding MenuItems}">
                <Menu.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Command" Value="{Binding RemoveCommand}" />
                        <Setter Property="CommandParameter" 
                           Value="{Binding CommandParameter}" />
                        <Setter Property="Header" Value="{Binding Header}" />
                        <!--<Setter Property="ItemsSource" Value="{Binding Items}"/>-->
                    </Style>
                </Menu.ItemContainerStyle>
            </Menu>
    
    
        public class FileMenu
        {
             public string CommandParameter { get; set; }
    
            public string Header { get; set; }
    
            ICommand _command;
    
            public ICommand RemoveCommand
            {
                get
                {
                    if (_command == null)
                    {
                        _command = new DelegateCommandMI(CanExecute, Execute);
                    }
                    return _command;
                }
            }
    
            private void Execute(object parameter)
            {
                string header = parameter.ToString();
    
            }
    
            private bool CanExecute(object parameter)
            {
                return true;
            }
        }
    
    

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年1月25日 11:05
    版主