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

本文介绍了如何在WPF中使用C#编程实现一个简单的登录界面,包括使用`INotifyPropertyChanged`接口监控属性变化,创建和绑定属性,以及处理用户输入事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、继承属性变化接口

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.创建被绑定属性

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

3.界面绑定属性

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

4.界面数据关联属性

        public MainWindow()
        {
            InitializeComponent();

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

5.使用绑定属性

// 获取界面输入
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 = "";
}

完整代码

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 = "";
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值