WPF —— MVVM command如何传递参数

点击按钮把窗体关闭 把页面的控件传递到自定义指令的函数中

FindAncestor 找到该组件的祖先元素

AncestorType={x:Type Window} 祖先元素类型为window
CommandParameter 自定义指令传递参数

自定义指令

cs 复制代码
public class MyCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object p)
    {
        return true;
    }

    public void Execute(object p)
    {
        var window = p as Window;
        if (window != null)
        {
            window.Close();
        }
    }
}

public MainWindow()

{

InitializeComponent();

M1 = new MyCommand();

this.DataContext = this;

//把指令封装公共类里面

//M1 = new CMD();

//this.DataContext = this;

}

public MyCommand M1 { get;set; }

//public CMD M1 { get; set; } 把指令封装公共类里面

相关推荐
dotent·1 天前
C#基于WPF UI框架的通用基础上位机测试WPF框架
ui·c#·wpf
咩图3 天前
WPF+Prism8.0.0.1909+C#创建一个桌面程序
c#·wpf·prism
雁于飞3 天前
分布式基础
java·spring boot·分布式·spring·wpf·cloud native
oioihoii3 天前
WPF入门指南:解析默认项目结构
wpf
极客智造3 天前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Macbethad4 天前
使用WPF编写一个多维度伺服系统的程序
大数据·hadoop·wpf
lingxiao168885 天前
WPF Prism框架应用
c#·wpf·prism
Macbethad5 天前
使用WPF编写一个Ethercat主站的程序
wpf
难搞靓仔5 天前
WPF 弹出窗体Popup
wpf·popup
Macbethad5 天前
使用WPF编写一个MODBUSTCP通信的程序
wpf