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.实验结果

相关推荐
qiangshang99012610 小时前
WPF+MVVM入门学习
学习·wpf
DASXSDW10 小时前
Abp vNext-事件总线使用实现及解析
ui·wpf
纸照片10 小时前
【邪修玩法】如何在WPF中开放 RESTful API 服务
后端·wpf·restful
啊丢_13 小时前
WPF基本布局容器与控件
wpf
c#上位机16 小时前
wpf之RelativeSource用法总结
c#·wpf
BingeBlog17 小时前
[01] Qt的UI框架选择和对比
开发语言·c++·笔记·qt·ui·开源软件
jh_cao1 天前
(1)SwiftUI 的哲学:声明式 UI vs 命令式 UI
ui·swiftui·命令模式
心疼你的一切2 天前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
我的xiaodoujiao2 天前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
Larry_Yanan2 天前
QML学习笔记(二十四)QML的Keys附加属性
c++·笔记·qt·学习·ui