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

    }
}

截图


相关推荐
avi91113 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理
吾与谁归in4 天前
C#脚本工具,支持智能提示自动补全(WPF + Roslyn + AvalonEdit)
开发语言·c#·wpf
昂好的day4 天前
WPF 从零入门(上):WPF 是什么、XAML 语法、布局与样式全解
开发语言·c#·wpf
影寂ldy5 天前
WPF导航窗体+Page页面跳转 知识点整理
wpf
He BianGu5 天前
【WPF-Control教程】IOC 依赖注入
c#·wpf·wpf-control
He BianGu5 天前
【WPF-Control教程】H.Extensions.Behvaiors 开发文档
c#·wpf·wpf-control
He BianGu5 天前
【WPF-Control教程】H.Style 开发文档
c#·wpf·wpf-control
麻瓜code5 天前
分布式锁演进:从一个用户下两单,到 Redisson,我改了四版
分布式·wpf
He BianGu5 天前
【WPF-Control教程】MVVM 二次开发文档
c#·wpf·wpf-control
He BianGu5 天前
【WPF-Control教程】Theme 主题系统二次开发文档
c#·wpf·wpf-control