WPF(2)命令绑定
效果是:当TextBox控件的Text属性为空时show按钮不可用,有值时show按钮可用
项目结构
界面代码
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <Label Content="输入的值" HorizontalAlignment="Left" Margin="68,52,0,0" VerticalAlignment="Top" Height="35" Width="69"/> <!--TextWrapping="Wrap" 内容是否换行,KeyUp="txtTitle_KeyUp"--> <!--UpdateSourceTrigger=PropertyChanged} 文本框里输入的值会实时更新到绑定的属性中--> <TextBox Name="txtTitle" Text="{Binding Title,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="142,52,0,242.667" TextWrapping="Wrap" Width="123"/> <Button Content="SHOW" Command="{Binding ValueCommand}" CommandParameter="123" HorizontalAlignment="Left" Margin="285,55,0,0" VerticalAlignment="Top" Width="78" Height="23"/> <!-- 控件数据的绑定--> <Label Content="{Binding Title}" HorizontalAlignment="Left" Margin="142,87,0,0" VerticalAlignment="Top"/> </Grid> </Window>
主窗体 视图模型
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using WpfApp1.Base; namespace WpfApp1.ViewModels { /// <summary> /// 主窗体 视图模型 /// </summary> public class MainViewModel : INotifyPropertyChanged { public MainViewModel() { _valueCommand = new CommandBase() { DoAction = new Action<object>(ValueCommandAction), DoCanExecute = new Func<object, bool>(CanExecute) }; } public event PropertyChangedEventHandler PropertyChanged; private string _title; public string Title { get { return _title; } set { _title = value; //数据更新 通知界面绑定的地方更新数据 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Title")); (this.ValueCommand as CommandBase).RaiseCanChanged(); } } private ICommand _valueCommand; /// <summary> /// 命令行为 /// </summary> public ICommand ValueCommand { get { return _valueCommand; } set { _valueCommand = value; } } private void ValueCommandAction(object obj) { //obj = CommandParameter Title = obj.ToString(); } private bool CanExecute(object obj) { return !string.IsNullOrEmpty(Title); } } }
CommandBase
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace WpfApp1.Base { /// <summary> /// 命令行为 基类 /// </summary> public class CommandBase : ICommand { public event EventHandler CanExecuteChanged; /// <summary> /// 是否可执行 /// </summary> /// <param name="parameter"></param> /// <returns></returns> public bool CanExecute(object parameter) { return DoCanExecute.Invoke(parameter); } /// <summary> /// 执行逻辑 /// </summary> /// <param name="parameter"></param> public void Execute(object parameter) { //控制逻辑 DoAction?.Invoke(parameter); } /// <summary> /// 作什么事 /// 处理业务逻辑 /// </summary> public Action<object> DoAction { get; set; } /// <summary> /// 是否可执行 /// 参数是object ,返回值是bool /// </summary> public Func<object, bool> DoCanExecute { get; set; } public void RaiseCanChanged() { CanExecuteChanged?.Invoke(this,new EventArgs()); } } }