• WPF(2)命令绑定


    效果是:当TextBox控件的Text属性为空时show按钮不可用,有值时show按钮可用

    项目结构

    界面代码

    1. <Window x:Class="WpfApp1.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfApp1"
    7. mc:Ignorable="d"
    8. Title="MainWindow" Height="350" Width="525">
    9. <Grid>
    10. <Label Content="输入的值" HorizontalAlignment="Left" Margin="68,52,0,0" VerticalAlignment="Top" Height="35" Width="69"/>
    11. <!--TextWrapping="Wrap" 内容是否换行,KeyUp="txtTitle_KeyUp"-->
    12. <!--UpdateSourceTrigger=PropertyChanged} 文本框里输入的值会实时更新到绑定的属性中-->
    13. <TextBox Name="txtTitle" Text="{Binding Title,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="142,52,0,242.667" TextWrapping="Wrap" Width="123"/>
    14. <Button Content="SHOW" Command="{Binding ValueCommand}" CommandParameter="123" HorizontalAlignment="Left" Margin="285,55,0,0" VerticalAlignment="Top" Width="78" Height="23"/>
    15. <!-- 控件数据的绑定-->
    16. <Label Content="{Binding Title}" HorizontalAlignment="Left" Margin="142,87,0,0" VerticalAlignment="Top"/>
    17. </Grid>
    18. </Window>

    主窗体 视图模型

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. using System.Windows.Input;
    8. using WpfApp1.Base;
    9. namespace WpfApp1.ViewModels
    10. {
    11. /// <summary>
    12. /// 主窗体 视图模型
    13. /// </summary>
    14. public class MainViewModel : INotifyPropertyChanged
    15. {
    16. public MainViewModel()
    17. {
    18. _valueCommand = new CommandBase() {
    19. DoAction = new Action<object>(ValueCommandAction),
    20. DoCanExecute = new Func<object, bool>(CanExecute)
    21. };
    22. }
    23. public event PropertyChangedEventHandler PropertyChanged;
    24. private string _title;
    25. public string Title
    26. {
    27. get { return _title; }
    28. set {
    29. _title = value;
    30. //数据更新 通知界面绑定的地方更新数据
    31. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Title"));
    32. (this.ValueCommand as CommandBase).RaiseCanChanged();
    33. }
    34. }
    35. private ICommand _valueCommand;
    36. /// <summary>
    37. /// 命令行为
    38. /// </summary>
    39. public ICommand ValueCommand
    40. {
    41. get { return _valueCommand; }
    42. set { _valueCommand = value; }
    43. }
    44. private void ValueCommandAction(object obj)
    45. {
    46. //obj = CommandParameter
    47. Title = obj.ToString();
    48. }
    49. private bool CanExecute(object obj)
    50. {
    51. return !string.IsNullOrEmpty(Title);
    52. }
    53. }
    54. }

    CommandBase

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Windows.Input;
    7. namespace WpfApp1.Base
    8. {
    9. /// <summary>
    10. /// 命令行为 基类
    11. /// </summary>
    12. public class CommandBase : ICommand
    13. {
    14. public event EventHandler CanExecuteChanged;
    15. /// <summary>
    16. /// 是否可执行
    17. /// </summary>
    18. /// <param name="parameter"></param>
    19. /// <returns></returns>
    20. public bool CanExecute(object parameter)
    21. {
    22. return DoCanExecute.Invoke(parameter);
    23. }
    24. /// <summary>
    25. /// 执行逻辑
    26. /// </summary>
    27. /// <param name="parameter"></param>
    28. public void Execute(object parameter)
    29. {
    30. //控制逻辑
    31. DoAction?.Invoke(parameter);
    32. }
    33. /// <summary>
    34. /// 作什么事
    35. /// 处理业务逻辑
    36. /// </summary>
    37. public Action<object> DoAction { get; set; }
    38. /// <summary>
    39. /// 是否可执行
    40. /// 参数是object ,返回值是bool
    41. /// </summary>
    42. public Func<object, bool> DoCanExecute { get; set; }
    43. public void RaiseCanChanged()
    44. {
    45. CanExecuteChanged?.Invoke(this,new EventArgs());
    46. }
    47. }
    48. }

    END

  • 相关阅读:
    node知识点(1)
    五大架构风格
    280049flash guide中文
    NSSCTF web刷题记录4
    【面试题】作用域和闭包
    每日一练 | 华为认证真题练习Day120
    图像分割 - 孤立点的检测
    Vue纯CSS实现掷色子
    时序预测 | MATLAB实现贝叶斯优化CNN-GRU时间序列预测(股票价格预测)
    从一个表格render方法问题看React函数组件的更新
  • 原文地址:https://blog.csdn.net/cjh16606260986/article/details/136613793