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

    }
}

截图


相关推荐
lindexi3 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~19 小时前
WPF---数据模版
wpf
hqwest2 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest2 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple2 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题3 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
开发WPF项目时遇到的问题总结
wpf
hqwest5 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel
Vae_Mars5 天前
WPF中使用InputBindings进行快捷键绑定
wpf
hqwest5 天前
C#WPF实战出真汁05--左侧导航
开发语言·c#·wpf·主界面·窗体设计·视图viewmodel