思路,将这个view当作参数传递给viewModel
步骤一:给xaml起名字:Name="window"
步骤二:在需要实现关闭的按钮中将window元素传出去
XML
<Button Command="{Binding CloseWindowCommand}" CommandParameter="{Binding ElementName=window}">立即注册</Button>
步骤三:viewModel中将传入的object类型转换一下然后就调用Close
方法:
cs
private void CloseWindow(object parameter)
{
MessageBox.Show("OK!");
var window = parameter as Window;
if (window != null)
{
window.Close();
}
}
构造函数中绑定按钮的Command:
cs
CloseWindowCommand = new RelayCommand<object>(t => CloseWindow(t));