WPF 绑定之静态与非静态属性更改

摘要:
<本地;&书信电报;使用System.ComponentModel;OnPropertyChanged(“myString”);newPropertyChangedEventArgs(propertyName));publicpartialclassMainWindow;设置DataContext=此;

 源码下载地址:https://github.com/lizhiqiang0204/Static-and-non-static-property-changes

程序集整体结构如下

WPF 绑定之静态与非静态属性更改第1张

 MainWindow.xaml布局如下

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel >
            <Label Content="{Binding myClassA.myString}"/>
        </StackPanel>

        <StackPanel Grid.Row="1" >
            <StackPanel.DataContext>
                <!--该Grid所有绑定都是绑定到ClassA里面的属性-->
                <local:ClassA/>
            </StackPanel.DataContext>
            <Label Content="{Binding myLabel}"/>

            <!--绑定到ClassA里面myLabel字符串属性-->
            <Button x:Name="myButton" Content="ButtonA" Click="ButtonA_Click" />
            <!--在MainWindow.xaml.cs后台文件中执行按键单击事件,单击事件就是改变ClassA中的myLabel属性同时在界面上显示出更改的信息-->
        </StackPanel>
    </Grid>

ClassA.cs如下

using System;
using System.ComponentModel;

namespace WpfApp1
{
    public class ClassA : INotifyPropertyChanged
    {
        private static string label = "静态字符";
        public static string myLabel
        {
            get { return label; }
            set
            {
                label = value;
                StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(nameof(myLabel)));//异步更新静态属性
            }
        }
        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;//静态事件处理属性更改

        private  string str = "非静态字符";
        public  string myString
        {
            get { return str; }
            set
            {
                str = value;
                OnPropertyChanged("myString");//异步更新非静态属性
            }
        }
        
        public event PropertyChangedEventHandler PropertyChanged;//非静态属性更改
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
MainWindow.xaml.cs如下
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ClassA myClassA { get; set; } = new ClassA();
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void ButtonA_Click(object sender, RoutedEventArgs e)
        {
            ClassA.myLabel = "更改绑定静态字符";
            myClassA.myString = "更改绑定非静态字符串";
        }
    }
}

单击按键之前界

WPF 绑定之静态与非静态属性更改第2张

 单击之后

WPF 绑定之静态与非静态属性更改第3张

免责声明:文章转载自《WPF 绑定之静态与非静态属性更改》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何设置CentOS 7获取动态IP和静态IPLinux查看系统信息的一些命令及查看已安装软件包的命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android开发 Html工具类详解

前言  在一些需求富文本显示或者编辑的开发情况下,数据都是用html的格式来保存文本信息的.而google是有提供解析html的工具类那就是Html.有了Html可以让TextView也支持富文本(其实原理还是解析Html然后在转成SpannableString再给TextView显示) 显示Html格式文本 String htmlConte...

js前端使用jOrgChart插件实现组织架构图的展示

项目要做组织架构图,要把它做成自上而下的树形结构。  需要购买阿里云产品和服务的,点击此链接领取优惠券红包,优惠购买哦,领取后一个月内有效: https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=fp9ccf07 一、说明 (1)通过后台查询数据库,生成树形数组结构,返回到前台。...

SpringCloud 配置文件 application.yml和 bootstrap.yml区别

SpringCloud 配置文件 application.yml和 bootstrap.yml区别 bootstrap.yml 先于 application.yml 加载,也就是比application.yml先加载 bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用bootst...

springmvc总结(配置传递参数去除前后空格、参数绑定时处理日期)

1.属性为Integer时,前台表单不填,默认为null;属性为String,前台表单不填,默认为"";2.属性为Integer时,前台表单填空格,model封装为null;属性为String,前台表单填空格,model封装为"  ";3.属性为Integer,后台model封装时【去除】前后空格;属性为String,后台model封装时【不去除】前后空格...

快捷键 Msg消息

https://baike.baidu.com/item/MSG/16826909?fr=aladdin https://docs.microsoft.com/en-us/windows/desktop/api/winuser/ns-winuser-tagmsg https://blog.csdn.net/wh_2396/article/details/5...

抢火车票利器:分享一个抓取火车票转让信息的小程序

代码 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.ComponentModel;using System.Threading;using System.Text.Regul...