简述wpf双向绑定

 WPF(Windows Presentation Foundation) Windows 桌面应用程序的技术框架,而 MVVM(Model-View-ViewModel)WPF 应用程序中组织和管理代码的架构模式。

  用户界面(View)与业务逻辑(Model)分离,并引入一个中介层(ViewModel)

页面部分 :TextBox直接用Binding绑定即可使用 Button则需要引入Command进行绑定

  <TextBlock Text="用户名" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" />
  <TextBox Text="{Binding UserName}"  Grid.Row="0" Grid.Column="1" Margin="5"  />
  <TextBlock Text="密码" VerticalAlignment="Center" Grid.Row="1"  Grid.Column="0"   />
  <TextBox Text="{Binding Password}" Grid.Row="1"  Grid.Column="1" Margin="5"  />
  <Button x:Name="Login_Btn" Grid.Row="3" Grid.ColumnSpan="2" Content="登录" Command="{Binding LoginAction }" />

后台部分:只需要引入然后把引入的参数及其方法指向当前的DataContext

 public partial class MainWindow : Window
 {
     LoginVM loginVm; // LoginVM 类的实例,用于处理与登录界面相关的逻辑和数据
     public MainWindow()
     {
         InitializeComponent();

         loginVm = new LoginVM(); // LoginVM 类的实例,用于处理与登录界面相关的逻辑和数据
         this.DataContext = loginVm; // 将主窗口的 DataContext 设置为 loginVm,用于绑定界面元素与 loginVm 的属性和命令
     }
 }

方法及数据处理类:信息交流中心 ,参数方法都在此操作

 public class LoginVM : INotifyPropertyChanged
 // 实现 INotifyPropertyChanged 接口的类,用于在属性值改变时通知界面更新
 {
     public LoginVM()
     {
         // 在构造函数中初始化 LoginAction 和 RegisterAction 命令
         LoginAction = new RelayCommand(LoginFunc, CanLogionRxecute);
         RegisterAction = new RelayCommand(RegisterFunc, CanLogionRxecute);
     }
     public event PropertyChangedEventHandler PropertyChanged;// PropertyChanged 事件,当属性值发生改变时会触发该事件

     private void ReadChange(string propertyName)// 触发 PropertyChanged 事件的方法
                                                 // 参数 propertyName:发生改变的属性名
     {
         PropertyChangedEventHandler hander = PropertyChanged;// 获取 PropertyChanged 事件的委托列表
         if (hander != null) hander(this, new PropertyChangedEventArgs(propertyName));// 获取 PropertyChanged 事件的委托列表
     }
     private LoginModel LoginPar = new LoginModel();

     public string UserName
     {
         get { return LoginPar.UserName; }
         set
         {
             LoginPar.UserName = value;
             ReadChange("UserName");
         }
     }
     public string Password
     {
         get { return LoginPar.Password; }
         set
         {
             LoginPar.Password = value;
             ReadChange("Password");
         }
     }
     void RegisterFunc() { 
         //注册逻辑
     }
     void LoginFunc()
     {
         if (UserName == "admin" && Password == "88888888")
         {
             index index = new index();
             index.Show();
         }
         else
         {
             MessageBox.Show($"账号密码错误!!!");
             UserName = "";
             Password = "";
         }
     }
     bool CanLogionRxecute()
     {
         return true; // 实现注册的 CanExecute 逻辑
     }
     public ICommand LoginAction { get; }// 命令 绑定到注册按钮
     public ICommand RegisterAction { get; }
 }

参数

 internal class LoginModel
 {
     private string _UserName;
     private string _Password;
     public string UserName
     {
         get { return _UserName; }
         set
         {
             _UserName = value;
         }
     }
     public string Password
     {
         get { return _Password; }
         set
         {
             _Password = value;
         }
     }
 }

ICommand: 固定写法可照抄

 public class RelayCommand : ICommand
 {
     private readonly Action _execute;     // 要执行的操作(命令的主要功能)
     private readonly Func<bool> _canExecute; // 用于判断命令是否可执行的方法

     // ICommand 接口的事件,当命令的可执行状态发生变化时会触发该事件
     public event EventHandler CanExecuteChanged;

     // 构造函数,初始化 RelayCommand 实例
     // 参数 execute:要执行的操作(必需)
     // 参数 canExecute:用于判断命令是否可执行的方法(可选)
     public RelayCommand(Action execute, Func<bool> canExecute = null)
     {
         _execute = execute ?? throw new ArgumentNullException(nameof(execute));
         _canExecute = canExecute;
     }

     // 判断命令是否可执行的方法
     // 参数 parameter:命令的参数(可选)
     // 返回值:如果命令可以执行,返回 true,否则返回 false
     public bool CanExecute(object parameter)
     {
         return _canExecute == null || _canExecute();
     }

     // 执行命令的方法
     // 参数 parameter:命令的参数(可选)
     public void Execute(object parameter)
     {
         _execute();
     }
 }

当一个笔记  如有不足欢迎指正&&评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值