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

相关推荐
c#上位机7 小时前
wpf之Border
c#·wpf
SunflowerCoder7 小时前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
XXYBMOOO8 小时前
Qt UDP 通信类详解与实现
开发语言·网络·c++·qt·网络协议·ui·udp
周杰伦fans8 小时前
WPF中的DataContext以及常见的绑定方式
wpf
摘星编程11 小时前
Cursor Pair Programming:在前端项目里用 AI 快速迭代 UI 组件
前端·人工智能·ui·typescript·前端开发·cursorai
安卓开发者13 小时前
鸿蒙Next的UI国际化与无障碍适老化实践:构建全球包容的数字世界
ui·华为·harmonyos
笑尘pyrotechnic13 小时前
push pop 和 present dismiss
macos·ui·ios·objective-c·cocoa
aiden:)15 小时前
App UI 自动化环境搭建指南
运维·python·ui·appium·自动化
叶玳言1 天前
【LVGL】从HTML到LVGL:嵌入式UI的设计迁移与落地实践
前端·ui·html·移植
我命由我123451 天前
Photoshop - Photoshop 触控手势
笔记·学习·ui·设计·photoshop·ps·美工