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

前言

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

1、读写属性更新界面

改变属性isStatus1的值就可以改变控件CheckBox的状态

csharp 复制代码
public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
    }
csharp 复制代码
 public class ViewModel : ViewModelBase
    {
        private bool _isStatus1 = false;

        public bool isStatus1
        {
            get
            {
                return _isStatus1;
            }

            set
            {
                _isStatus1 = value;
                OnPropertyChanged("isStatus1");
            }
        }
    }
csharp 复制代码
<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 控件更新。

csharp 复制代码
 public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
    }
csharp 复制代码
 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 "空闲";
                }
            }

        }
    }
csharp 复制代码
<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数据库从入门到精通》

相关推荐
小北方城市网9 小时前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf
暮疯不疯15 小时前
C#常见术语表格
开发语言·c#
JQLvopkk16 小时前
VS2015使用C#连接KepserverEX并操作读写节点
开发语言·c#
流水线上的指令侠18 小时前
补充说明——针对《C#:从 0 到 1 创建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目》
功能测试·ui·c#·自动化·wpf
流水线上的指令侠19 小时前
C# 实战:从 0 到 1 搭建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目
功能测试·ui·c#·自动化·wpf·visual studio
gc_229920 小时前
学习C#调用OpenXml操作word文档的基本用法(20:学习嵌入文件类)
c#·word·openxml·嵌入文档
玩泥巴的20 小时前
如何实现一套.net系统集成多个飞书应用
c#·.net·二次开发·飞书
ghie909020 小时前
基于C#实现俄罗斯方块游戏
开发语言·游戏·c#
ccut 第一混20 小时前
C# 基于 RS485 与设备通讯(以照度计为例子)
c#·rs485