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

相关推荐
wuk9989 小时前
Android:UI:Drawable:View/ImageView与Drawable
android·ui
freesheep72010 小时前
WPF使用PreviewTextInput事件限制用户输入
c#·wpf
dzj202114 小时前
VS Code中配置使用slint(Rust)的一个小例子
ui·rust·slint
天下无贼!19 小时前
【自制组件库】从零到一实现属于自己的 Vue3 组件库!!!
前端·javascript·vue.js·ui·架构·scss
慧都小项21 小时前
UI测试平台TestComplete如何实现从Git到Jenkins的持续测试
git·ui·jenkins·代码质量·testcomplete·zephyr for jira
c#上位机2 天前
wpf之ControlTemplate
wpf
脑袋大大的2 天前
uni-app x开发避坑指南:拯救被卡顿的UI线程!
开发语言·前端·javascript·vue.js·ui·uni-app·uts
zh_xuan2 天前
duiLib 自定义资源目录
c++·ui
栗子味清清3 天前
Axure入门指南:功能讲解、快捷键整理与操作思维全拆解
ui·axure·photoshop