zoukankan      html  css  js  c++  java
  • wpf mvvm MenuItem的Command事件

    这是一个事件的辅助类,可以通过它实现MenuItem的Command事件

    public class MyCommands : Freezable, ICommand, ICommandSource
        {
    
            public MyCommands() 
            {
            }
    
            public static readonly DependencyProperty CommandParameterProperty =
              DependencyProperty.Register(
                  "CommandParameter",
                  typeof(object),
                  typeof(MyCommands),
                  new PropertyMetadata((object)null));
    
            public object CommandParameter
            {
                get
                {
                    return (object)GetValue(CommandParameterProperty);
                }
                set
                {
                    SetValue(CommandParameterProperty, value);
                }
            }
    
            public static readonly DependencyProperty CommandTargetProperty =
               DependencyProperty.Register(
                   "CommandTarget",
                   typeof(IInputElement),
                   typeof(MyCommands),
                   new PropertyMetadata((IInputElement)null));
    
            public IInputElement CommandTarget
            {
                get
                {
                    return (IInputElement)GetValue(CommandTargetProperty);
                }
                set
                {
                    SetValue(CommandTargetProperty, value);
                }
            }
    
            public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MyCommands), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));
    
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
    
            #region ICommand Members
    
            public bool CanExecute(object parameter)
            {
                if (Command != null)
                    return Command.CanExecute(CommandParameter);
                return false;
            }
    
            public void Execute(object parameter)
            {
                Command.Execute(CommandParameter);
            }
    
            public event EventHandler CanExecuteChanged;
    
            private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                MyCommands commandReference = d as MyCommands;
                ICommand oldCommand = e.OldValue as ICommand;
                ICommand newCommand = e.NewValue as ICommand;
    
                if (oldCommand != null)
                {
                    oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
                }
                if (newCommand != null)
                {
                    newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
                }
            }
    
            #endregion
    
            #region Freezable
    
            protected override Freezable CreateInstanceCore()
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }

    在xaml中调用的方法

    <UserControl.Resources>
            <unititys:MyCommands x:Key="aaa" Command="{Binding Path=aaa}"/>
    </UserControl.Resources>
    
    
     <ContextMenu x:Key="RouterMenu1" DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.DataContext}">
                <MenuItem Header="调用aaa" Command="{StaticResource aaa}"></MenuItem>
            </ContextMenu>

    在ViewModel中和普通的Command一样的调用就行了

  • 相关阅读:
    ios 一个正则表达式测试(只可输入中文、字母和数字)
    IOS7 8中tableview分割线缺少15像素
    Java中使用OpenSSL生成的RSA公私钥进行数据加解密
    java与IOS之间的RSA加解密
    ios下使用rsa算法与php进行加解密通讯
    C# 32位md5
    [原]命令模式在MVC框架中的应用
    [原]【推荐】程序员必读的三十本经典巨作
    [原]容器学习(二):动手模拟AOP
    [原]容器学习(一):动手模拟spring的IoC
  • 原文地址:https://www.cnblogs.com/chenjinshi/p/4645081.html
Copyright © 2011-2022 走看看