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());
        }
    }
}

END

相关推荐

  1. WPF 界面命令(MVVM结构)

    2024-03-12 23:10:03       37 阅读
  2. WPF数据

    2024-03-12 23:10:03       37 阅读
  3. WPF.NET开发】

    2024-03-12 23:10:03       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-12 23:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-12 23:10:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-12 23:10:03       82 阅读
  4. Python语言-面向对象

    2024-03-12 23:10:03       91 阅读

热门阅读

  1. 第一次Python小练习题目

    2024-03-12 23:10:03       55 阅读
  2. 03_Tomcat

    03_Tomcat

    2024-03-12 23:10:03      35 阅读
  3. Android中向Fragment传递数据

    2024-03-12 23:10:03       39 阅读
  4. leetcode - 953. Verifying an Alien Dictionary

    2024-03-12 23:10:03       41 阅读
  5. 使用hashmap优化时间复杂度,leetcode1577

    2024-03-12 23:10:03       46 阅读
  6. 计算机等级考试:信息安全技术 知识点六

    2024-03-12 23:10:03       43 阅读
  7. 【Go】探索Go语言中的关于defer的应用

    2024-03-12 23:10:03       45 阅读
  8. VSCode无法用ctrl+鼠标滚轮调整字体大小了

    2024-03-12 23:10:03       37 阅读
  9. python中的幂运算

    2024-03-12 23:10:03       40 阅读