当前位置: 首页 > news >正文

WPF中如何在MVVM模式下关闭窗口

完全来源于十月的寒流,感谢大佬讲解

使用Behaviors

<Window x:Class="Test_03.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:b="http://schemas.microsoft.com/xaml/behaviors"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Test_03"WindowStyle="None"mc:Ignorable="d"Height="450" Width="800"><Window.DataContext><local:MainWindowViewModel></local:MainWindowViewModel></Window.DataContext><Grid><TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Text="{Binding Title}"></TextBlock><StackPanel Margin="5.0" HorizontalAlignment="Right" VerticalAlignment="Top"Orientation="Horizontal"><StackPanel.Resources></StackPanel.Resources><Button Margin="5.0" Content="_"><b:Interaction.Triggers><b:EventTrigger EventName="Click"><b:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" PropertyName="WindowState" Value="{x:Static WindowState.Minimized}"></b:ChangePropertyAction></b:EventTrigger></b:Interaction.Triggers></Button><Button Margin="5.0" Content="[ ]"><b:Interaction.Triggers><b:EventTrigger EventName="Click"><b:ChangePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" PropertyName="WindowState" Value="{x:Static WindowState.Maximized}"></b:ChangePropertyAction></b:EventTrigger></b:Interaction.Triggers></Button><Button Margin="5.0" Content="X" Background="Red"><b:Interaction.Triggers><b:EventTrigger EventName="Click"><b:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" MethodName="Close"></b:CallMethodAction></b:EventTrigger></b:Interaction.Triggers></Button></StackPanel></Grid>
</Window>

讲解

  1. <b:Interaction.Triggers>: 是一个开始定义触发器的标记。通常,这个标记包含在一个控件的开始标记和结束标记之间,用于指定将要应用触发器的控件。

  2. <b:EventTrigger EventName="Click">: 表示一个事件触发器,告诉XAML在指定的事件发生时执行后续定义的动作。在这里,事件名称为"Click",表示当用户点击与此触发器相关联的UI元素时触发。

  3. <b:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}" MethodName="Close"></b:CallMethodAction>: 这是触发器的动作部分。使用了CallMethodAction,这是一种行为,用于调用方法。指定了要调用的方法是窗口(Window)的Close方法。

    • TargetObject="{Binding RelativeSource={RelativeSource AncestorType=Window}}": 这部分指定了目标对象,即将要调用方法的对象。在这里,通过使用RelativeSource,找到了最近的窗口(Window)并将其绑定为目标对象。

    • MethodName="Close": 这部分指定了要调用的方法的名称,即窗口的Close方法。

因此,整个代码的作用是,当与此触发器相关联的UI元素(可能是一个按钮)被点击时,会调用最近的窗口的Close方法,关闭该窗口。

http://www.lryc.cn/news/235971.html

相关文章:

  • 【数据结构&C++】二叉平衡搜索树-AVL树(25)
  • Python算法——树的最大深度和最小深度
  • 46.全排列-py
  • 系列三、GC垃圾回收算法和垃圾收集器的关系?分别是什么请你谈谈
  • WPF中的虚拟化是什么
  • 免费稳定几乎无门槛,我的ChartGPT助手免费分享给你
  • 奇瑞金融:汽车金融行业架构设计
  • milvus数据库分区管理
  • pytorch.nn.Conv1d详解
  • 大数据HCIE成神之路之数学(2)——线性代数
  • 音视频学习(十八)——使用ffmepg实现视音频解码
  • nginx的GeoIP模块
  • mac控制台命令小技巧
  • Postman:API测试之Postman使用完全指南
  • Flume学习笔记(3)—— Flume 自定义组件
  • go的字符切片和字符串互转
  • 所见即所得的动画效果:Animate.css
  • ERR:Navicat连接Sql Server报错
  • python算法例10 整数转换为罗马数字
  • springboot引入第三方jar包放到项目目录中,添加web.xml
  • 大数据研发工程师课前环境搭建
  • Qt图形视图框架:QGraphicsItem详解
  • defer和async
  • 数电实验-----实现74LS139芯片扩展为3-8译码器以及应用(Quartus II )
  • 洋葱架构、三层架构及两者区别
  • JavaEE进阶学习:Spring 的创建和使用
  • 音视频项目—基于FFmpeg和SDL的音视频播放器解析(十四)
  • Tomcat无法映射到activiti-app导致activiti无法启动页面
  • c语言常见的面试问题
  • image图片之间的间隙消除