wcf 简单实践 数据绑定 数据更新ui

1.概要

2.代码

2.1 xaml

复制代码
<Window x:Class="WpfApp3.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:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="300">
    <StackPanel>
        <TextBox Text="{Binding Path=Zhi}"></TextBox>
        <Button Content="Button"  Click="Button_Click"/>
    </StackPanel>
</Window>

2.2 code

复制代码
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// 
    /// </summary>
    public partial class MainWindow : Window
    {
        A a = new A();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = a;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.a.Zhi = "新的值";
        }
    }
    class A:INotifyPropertyChanged
    {
        public A()
        {
            this.Zhi = "旧的值";
        }
        private String _zhi;

        public String Zhi {
            set { _zhi = value; OnPropertyChanged(nameof( Zhi));
            } 
            get { 
                return _zhi; 
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged(String name)
        {
            if (PropertyChanged != null) { 
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

3.实验结果

相关推荐
yiruwanlu15 小时前
乡村文旅设计师推荐:建筑设计能力筛选要点解析
python·ui
晓纪同学15 小时前
WPF-09 命令系统
wpf
晓纪同学17 小时前
WPF-10资源系统
wpf
jf加菲猫20 小时前
第12章 数据可视化
开发语言·c++·qt·ui
麒麟ZHAO21 小时前
鸿蒙flutter第三方库适配 - 服务端驱动UI应用
flutter·ui·华为·harmonyos
报错小能手1 天前
SwiftUI 框架 认识 SwiftUI 视图结构 + 布局
ui·ios·swift
七夜zippoe1 天前
DolphinDB集群部署:从单机到分布式
分布式·wpf·单机·dolphindb·分集群
波波0072 天前
写出稳定C#系统的关键:不可变性思想解析
开发语言·c#·wpf
bugcome_com2 天前
从 MVVMLight 到 CommunityToolkit.Mvvm:MVVM 框架的现代化演进与全面对比
wpf
星辰即远方2 天前
UI学习2
学习·ui