点击按钮把窗体关闭 把页面的控件传递到自定义指令的函数中
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; } 把指令封装公共类里面