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一样的调用就行了

时间: 2024-08-05 15:23:28

wpf mvvm MenuItem的Command事件的相关文章

WPF MVVM DataGrid Button Command绑定

<Button Content="删除" Background="{StaticResource mainColor}" Width="90" Height="35" Style="{StaticResource BtnStyle}" Command="{Binding Path=DataContext.ExecuteDelDrugCommand, RelativeSource= {Rela

WPF - MVVM - 如何将ComboBox的Selectchange事件binding到ViewModel

将所有的事件,属性,都映射到ViewModel中.好处多多,以后开发尽量用这种模式. 解决方法: 使用System.Windows.Interactivity.dll,添加该dll到项目引用 ? 1 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ComboBox映射的代码: <ComboBox VerticalAlignment="Ce

从PRISM开始学WPF(六)MVVM(三)事件聚合器EventAggregator?

从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? 从PRISM开始学WPF(五)MVVM(一)ViewModel? 从PRISM开始学WPF(六)MVVM(二)Command? 从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator? Event aggregation. For communication acros

从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator?

原文:从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator? 从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? 从PRISM开始学WPF(五)MVVM(一)ViewModel? 从PRISM开始学WPF(六)MVVM(二)Command? 从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggrega

WPF MVVM从入门到精通4:命令和事件

原文:WPF MVVM从入门到精通4:命令和事件 ? WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM从入门到精通3:数据绑定 WPF MVVM从入门到精通4:命令和事件 WPF MVVM从入门到精通5:PasswordBox的绑定 WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定 WPF MVVM从入门到精通7:关闭窗口和打开新窗口 WPF MVVM从入门到精通8:数据验证 完整示例代码下载LoginD

.NET Core 3 WPF MVVM框架 Prism系列之命令

原文:.NET Core 3 WPF MVVM框架 Prism系列之命令 本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的命令的用法 一.创建DelegateCommand命令# 我们在上一篇.NET Core 3 WPF MVVM框架 Prism系列之数据绑定中知道prism实现数据绑定的方式,我们按照标准的写法来实现,我们分别创建Views文件夹和ViewModels文件夹,将MainWindow放在Views文件夹下,再在ViewModels文件夹下面创建MainWi

WPF MVVM初体验

首先MVVM设计模式的结构, Views: 由Window/Page/UserControl等构成,通过DataBinding与ViewModels建立关联: ViewModels:由一组命令,可以绑定的属性,操作逻辑构成:因为View与ViewModel进行了解耦,我们可以对ViewModel进行Unit Test: Models:可以是实体对象或者Web服务: 下面通过一个简单的例子,来介绍一些WPF MVVM模式.示例将展示一个图片浏览器,打开图片,放大/缩小图片大小.首先项目结构: UI

WPF MVVM 架构 Step By Step(6)(把actions从view model解耦)

到现在为止,我们创建了一个简单的MVVM的例子,包含了实现了的属性和命令.我们现在有这样一个包含了例如textbox类似的输入元素的视图,textbox用绑定来和view model联系,像点击button这样的行为用命令来联系.view model和model在内部通信. 但是在上面的架构中有一个问题,command类和view model有很严重的耦合.如果你记得command类的代码(在下面也有展示),在构造函数中传递view model对象,意味着这个command 类不能再其他的vie

WPF MVVM TreeView 实现 右键选中 右键菜单

1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown="TreeViewItem_PreviewMouseRightButtonDown" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAli