【WPF】数据绑定之---单向绑定

wpf数据单向绑定

代码展示

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
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_login
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public string _userName;

        public string _password;
        public string userName
        {
            get { return _userName; }

            set { _userName = value;
                RaisePropertyChanged("userName");
            }
        }


        public string pwd
        {
            get { return _password; }

            set
            {
                _password = value;
                RaisePropertyChanged("pwd");
            }
        }

        //声明事件
        public event PropertyChangedEventHandler PropertyChanged;

        //引发事件的方法:
//        RaisePropertyChanged 方法接受一个字符串参数 PropertyName,表示发生变化的属性名。
//方法首先获取 PropertyChanged 事件的当前值(防止在调用时被其他线程修改)。
//如果有订阅者(handler 不为 null),则通过调用 handler 触发 PropertyChanged 事件,通知所有订阅者指定的属性已发生变化。
        public void RaisePropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler handler= PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(PropertyName));
            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (userName != "666"&&pwd !="666")
            {
                MessageBox.Show("账号错误");

                userName = "";
                pwd = "";
                return;
            }
            else
            {
                MessageBox.Show("登录成功");
            }
        }

    }
}

截图


相关推荐
凌镜icecraft4 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
光泽雨9 天前
控件Password附加属性
wpf
samble13 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble22 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴23 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67524 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教24 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨1 个月前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟1 个月前
WPF(数据绑定)
wpf
samble1 个月前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例