0
点赞
收藏
分享

微信扫一扫

外包干了一个月,技术明显进步。。。

生命中最美的是成长 2024-05-02 阅读 6
wpf

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

FindAncestor 找到该组件的祖先元素
        AncestorType={x:Type Window} 祖先元素类型为window
         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()
 {
     InitializeComponent();
     M1 = new MyCommand();
     this.DataContext = this;

     //把指令封装公共类里面
     //M1 = new CMD();
     //this.DataContext = this; 
 }
 public MyCommand M1 { get;set; }
 //public CMD M1 { get; set; } 把指令封装公共类里面

举报

相关推荐

0 条评论