【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("登录成功");
            }
        }

    }
}

截图


相关推荐
zzyzxb19 小时前
WPF中Adorner和Style异同
wpf
棉晗榜21 小时前
WPF锚点页面,点击跳转到指定区域
wpf
zzyzxb1 天前
Style/Setter、Template 属性、ControlTemplate 三者的关系
wpf
要记得喝水1 天前
某公司WPF面试题(含答案和解析)--2
wpf
zzyzxb1 天前
WPF中Template、Style、Adorner异同
wpf
小股虫1 天前
数据一致性保障:从理论深度到架构实践的十年沉淀
架构·wpf
廋到被风吹走1 天前
【Spring】PlatformTransactionManager详解
java·spring·wpf
源之缘-OFD先行者1 天前
全栈开发实战:WPF+FFmpeg+GIS,打造工业级雷达探测终端
ffmpeg·wpf
Poetinthedusk2 天前
WPF动画制作分享
wpf·动画
张人玉2 天前
WPF HTTPS 通信示例使用说明
数据库·网络协议·http·c#·wpf