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 = "";
            }
        }
    }
}
相关推荐
烟话63 天前
MVVM核心机制:属性通知与命令绑定解析
wpf
不知名君3 天前
WPF 的原生窗体标题栏主题自适应系统深浅主题
wpf
碎碎念的安静3 天前
WPF 与 Qt 进程间通信(IPC)
开发语言·qt·wpf
军训猫猫头4 天前
7.带输入参数的线程启动 C# + WPF 完整示例
开发语言·前端·c#·.net·wpf
周杰伦fans5 天前
WPF Prism 框架完全入门指南:从环境搭建到弹窗导航实战
wpf
雨浓YN5 天前
WPF MVVM 模式(无调库)项目创建笔记
笔记·wpf
周杰伦fans5 天前
.NET AOT技术深度解析:为什么WPF不支持而Avalonia/UWP支持?
.net·wpf
雨浓YN5 天前
WPF MVVM 模式(调Prism库)项目创建笔记 —— 包含C++/CLI OpenCV互操作
c++·笔记·wpf
七夜zippoe5 天前
DolphinDB数据模型:表、分区与分布式表
分布式·wpf·数据模型··dolphindb
一念春风6 天前
Qwen2.5 (AI模型 PC搭建)
人工智能·ai·c#·wpf·模型