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

wpf之MVVM中只读属性更新界面

前言

在wpf程序中有时候某个属性是只读属性,该属性的值是根据其他属性获取的,只读属性本身无法更新界面,本来就来介绍如何在只读属性中更新界面。

1、读写属性更新界面

在这里插入图片描述
改变属性isStatus1的值就可以改变控件CheckBox的状态

public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propname){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propname));}}}
 public class ViewModel : ViewModelBase{private bool _isStatus1 = false;public bool isStatus1{get{return _isStatus1;}set{_isStatus1 = value;OnPropertyChanged("isStatus1");}}}
<Window x:Class="wpf之Ellipse.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"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:wpf之Ellipse"mc:Ignorable="d"Title="MVVM绑定只读属性" Height="450" Width="800"><Grid><StackPanel Orientation="Vertical"  ><CheckBox IsChecked="{Binding isStatus1  }"  Width=" 20" Height=" 20" Background="Green"  /></StackPanel ></Grid>
</Window>

2、 只读属性

2.1 不勾选CheckBox

在这里插入图片描述

2.1 勾选CheckBox

在这里插入图片描述
TextBlock 控件绑定的是Status属性,但是这个属性是一个只读属性,它是根据属性isStatus1的值来更新的,由于Status属性本身没有更新界面的机制,所以需要在isStatus1属性的Set方法中加入OnPropertyChanged(“Status”);这行代码来实现Status属性绑定的TextBlock 控件更新。

 public class ViewModelBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propname){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propname));}}}
 public class ViewModel : ViewModelBase{private bool _isStatus1 = false;public bool isStatus1{get{return _isStatus1;}set{_isStatus1 = value;OnPropertyChanged("isStatus1");OnPropertyChanged("Status");}}public string Status{get{if (isStatus1){return "状态1";}else{return "空闲";}}}}
<Window x:Class="wpf之Ellipse.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"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:wpf之Ellipse"mc:Ignorable="d"Title="MVVM绑定只读属性" Height="450" Width="800"><Grid><StackPanel Orientation="Vertical"  ><TextBlock Text="{Binding Status}"  Height=" 20" Background="Red"  TextAlignment="Center"    /><CheckBox IsChecked="{Binding isStatus1  }"  Width=" 20" Height=" 20" Background="Green"  /></StackPanel ></Grid>
</Window>

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.dtcms.com/a/465763.html

相关文章:

  • 南通企业免费建站深圳网站开发运营公司
  • php微信商家转账回调通知数据解密
  • 使用Linux的read和write系统函数操作文件
  • 基于 PLC 的仓储管理系统设计
  • 企业网站建设计划内部局域网怎么搭建
  • elasticsearch索引多长时间刷新一次(智能刷新索引根据数据条数去更新)
  • 脑电模型实战系列(二):PyTorch实现简单DNN模型
  • 脑电模型实战系列(二):为什么从简单DNN开始脑电情绪识别?
  • 哪个网站做h5比较好看金华手机建站模板
  • 制作网站源码电子商务网站建设课后习题答案
  • Google 智能体设计模式:模型上下文协议 (MCP)
  • 智能 DAG 编辑器:从基础功能到创新应用的全方位探索
  • 多语言建站系统深圳做网站比较好的公司有哪些
  • 基于OpenCV的智能疲劳检测系统:原理、实现与创新
  • Google 智能体设计模式:多智能体协作
  • 建设企业网站目的杭州网站建设q479185700惠
  • 自己建网站百度到吗网站建设与维护功能意义
  • Oracle 数据库多实例配置
  • 任天堂3DS模拟器最新版 Azahar Emulator 2123.3 开源游戏模拟器
  • 深圳福田网站建设公司共享ip网站 排名影响
  • 【AI安全】Anthropic推出AI安全工具Petri:通过自主Agent研究大模型行为
  • 云南做网站哪家便宜wordpress单页下载
  • 深度掌握 Git 分支体系:从基础操作到高级策略
  • CTF — ZIP 文件密码恢复
  • AI编程 | 基于即梦AI-Seedream 4.0模型,搭建人脸生成系统
  • 找设计案例的网站网站 设计
  • 医院项目:IBMS 集成系统 + 楼宇自控系统 + 智能照明系统协同解决方案
  • JavaEE初阶5.0
  • 一个企业做网站推广的优势手机网站怎么制作内容
  • 有代码怎么做网站做网站用源码