WPF 界面变量绑定(通知界面变化)

1、继承属性变化接口

csharp 复制代码
public partial class MainWindow : Window, INotifyPropertyChanged
{
		// 通知界面属性发生变化
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler != null) 
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
}

2.创建被绑定属性

csharp 复制代码
        // 创建被绑定的属性
        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value;
                // 通知界面 属性发生变化
                RaisePropertyChanged("UserName");
            }
        }

3.界面绑定属性

html 复制代码
            <!-- 绑定代码中的属性  UserName -->
            <TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="1" Margin="2"/>

4.界面数据关联属性

csharp 复制代码
        public MainWindow()
        {
            InitializeComponent();

            // 为界面设置 绑定数据
            this.DataContext = this;
        }

5.使用绑定属性

csharp 复制代码
// 获取界面输入
if (UserName == "WPF" && passWord == "123")
{   // 弹出一个新的界面 ctrl+ k + d
    //MessageBox.Show("OK");
    IndexWindow indexWindow = new IndexWindow();
    indexWindow.Show();

    // 隐藏登录界面
    this.Hide();
}
else
{// 警告框
    MessageBox.Show("输入的用户名或密码不正确");
    //txtUserName.Text = "";
    // 清空界面数据
    UserName = "";
    txtPassword.Text = "";
}

完整代码

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WPF_LoginUI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();

            // 为界面设置 绑定数据
            this.DataContext = this;
        }

        // 创建被绑定的属性
        private string _userName;

        public string UserName
        {
            get { return _userName; }
            set { _userName = value;
                // 通知界面 属性发生变化
                RaisePropertyChanged("UserName");
            }
        }

        // 被用来 界面绑定的属性
        //public string UserName { get; set; } = "WPF";

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if(handler != null) 
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// 按钮 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            //string userName = txtUserName.Text;
            string passWord = txtPassword.Text;

            if (UserName == "WPF" && passWord == "123")
            {   // 弹出一个新的界面 ctrl+ k + d
                //MessageBox.Show("OK");
                IndexWindow indexWindow = new IndexWindow();
                indexWindow.Show();

                // 隐藏登录界面
                this.Hide();
            }
            else
            {// 警告框
                MessageBox.Show("输入的用户名或密码不正确");
                //txtUserName.Text = "";
                UserName = "";
                txtPassword.Text = "";
            }
        }
    }
}
相关推荐
Poetinthedusk4 小时前
WPF获得当前电脑的储存和运存
wpf
unicrom_深圳市由你创科技5 小时前
Qt、MFC、WinForm、WPF,哪个做上位机界面更好?
qt·wpf·mfc
暮雪倾风21 小时前
【WPF】使用Costura.Fody将工程打包为单个EXE文件
wpf·exe·windows原生开发
咖啡の猫1 天前
Jedis快速入门
wpf
Scout-leaf1 天前
WPF新手村教程(五)— 附魔教学(绑定)
c#·wpf
数据知道2 天前
MongoDB灾难恢复计划:RTO/RPO目标下的应急响应完整方案
数据库·mongodb·wpf
闻哥3 天前
深入剖析Redis数据类型与底层数据结构
java·jvm·数据结构·spring boot·redis·面试·wpf
yatum_20143 天前
Hadoop 三种核心运行模式(伪分布式/分布式/混合模式)全总结
hadoop·分布式·wpf
有技巧搬砖3 天前
基于WPF MVVM的流程编排状态机引擎上位机
wpf·状态机·流程步骤
Wiktok3 天前
WPF核心UI组件的功能、使用场景和基础示例
ui·wpf