WPF —— MVVM command如何传递参数

慈云数据 2024-05-09 技术支持 23 0

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

FindAncestor 找到该组件的祖先元素

        AncestorType={x:Type Window} 祖先元素类型为window

WPF —— MVVM command如何传递参数
(图片来源网络,侵删)

         CommandParameter 自定义指令传递参数

自定义指令

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()

WPF —— MVVM command如何传递参数
(图片来源网络,侵删)

 {

     InitializeComponent();

     M1 = new MyCommand();

     this.dataContext = this;

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

     //M1 = new CMD();

     //this.DataContext = this; 

 }

 public MyCommand M1 { get;set; }

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

微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon