mvvm框架下,wpf如何实现窗口的关闭

思路,将这个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));
相关推荐
阿登林14 小时前
初步学习WPF-Prism
学习·wpf
△曉風殘月〆1 天前
WPF MVVM进阶系列教程(三、使用依赖注入)
wpf·mvvm
此wei浩亦1 天前
WPF中使用 using prism.region 报错
c#·wpf·prism
dotent·2 天前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机2 天前
wpf之ComboBox
wpf
lindexi2 天前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~3 天前
WPF---数据模版
wpf
hqwest4 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest4 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple4 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf