首页 > 其他分享 >WPF(2)命令绑定

WPF(2)命令绑定

时间:2023-01-29 14:55:53浏览次数:42  
标签:object Title 绑定 System 命令 new using WPF public

效果是:当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());
        }
    }
}

 

标签:object,Title,绑定,System,命令,new,using,WPF,public
From: https://www.cnblogs.com/stevenchen2016/p/17068858.html

相关文章

  • mysql 管理员常用命令
    1、创建用户createuseradmin@localhostidentifiedby'password';2、赋权grantprivilegesONdatabase.tableTO'username'[@'host'][withgrantoption]gr......
  • 使用Git操作Gitee命令
    使用Git操作Gitee命令安装Git略上传代码到Gitee首先,在Gitee上新建仓库,设置为公开;然后,打开GitBash,执行如下命令:gitconfig--globaluser.name"kylewang_ai"gi......
  • linux命令与公私钥
    昨日内容回顾etc目录配置相关/etc/profile 环境变量文件/etc/motd 开机欢迎界面usr目录程序相关四种安装软件的方式 1.yum安装 自动解决依赖性问题 2.r......
  • grep 命令详解
    grep简介grep[-acinv][--color=auto]'匹配的字串'文件名称选项与参数:-a :将二进制文件以text文件的方式匹配-c :统计匹配到的字串次数-i :忽略大小写-n :输出行号-v :......
  • shell/Linux 任务学习笔记整理2:head/tail命令
    注!!:笔记来源:(原文链接:)https://blog.csdn.net/zznnniuu/article/details/123155074      版权声明:本文为CSDN博主「zznnniuu」的原创文章原文链接:https://blog......
  • Git命令
    环境配置//配置用户信息gitconfig--globaluser.name"name"gitconfig--globaluser.email"email"//查看所有用户配置gitconfig--list//查看user.name配......
  • CentOS网络服务操作命令
    CentOS重启网络服务,输入下面的命令:systemctlretartnetwork.service或systemctlrestartnetwork。CentOS启动网络服务,输入下面的命令:systemctlstartnetwork.servi......
  • Linux-crontab定期执行程序的命令
    https://www.runoob.com/linux/linux-comm-crontab.html Linux crontab 是用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。crond 命......
  • linux命令2
    ⽹络不通排查流程1.确认⽹关地址是否通畅2.确认⽹卡配置是否正确3.确认⽹络管理服务关闭 systemctlstopNetworkManager systemctldisableNetworkManageret......
  • bash:"IF command"命令作为条件
    bash:"IFcommand"命令作为条件    一、说明 1、IF的条件部分,可以使用linux“命令”。1ifcommand2then3commandif$?=04else5command......