internal class DelegateCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute; public DelegateCommand(Action execute) : this(execute, null) { }
public DelegateCommand(Action execute, Func<bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
} public void Execute(object parameter)
{
_execute();
}
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute();
} public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
internal class DelegateCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<bool> _canExecute; public DelegateCommand(Action<T> execute) : this(execute, null) { }
public DelegateCommand(Action<T> execute, Func<bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
} public void Execute(object parameter)
{
_execute((T)parameter);
}
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute();
} public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
<Window x:Class="WpfApp21.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:viewModels="clr-namespace:WpfApp21.ViewModels"
xmlns:local="clr-namespace:WpfApp21"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<viewModels:SearchWordViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox x:Name="InputTextBox"></TextBox>
<Button Command="{Binding SearchCommand}" CommandParameter="{Binding ElementName=InputTextBox,Path=Text}"></Button>
<Image Source="{Binding Name}" />
</StackPanel>
</Window>
    public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public virtual void OnPropertyChanged([CallerMemberName] string PropertyChanged = null)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(PropertyChanged));
}
}
}
public class MainViewModel : ViewModelBase
{
private BitmapImage name;
public BitmapImage Name
{
get { return name; }
set { name = value; OnPropertyChanged(); }
} public MainViewModel()
{
Name = new BitmapImage(new System.Uri("https://dss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=1758912268,304734450&fm=55&app=54&f=JPEG?w=1140&h=640", System.UriKind.RelativeOrAbsolute)); }
}

c# button Command的更多相关文章

  1. Python3 tkinter基础 Button command 单击按钮 在console中打印文本

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. 在DataGrid中实现Button Command

    Command="{Binding butCommand}"会默认查找ListViewItems中对象的属性,而你的ListViewItems中对象应该不包括butCommand属 ...

  3. WPF – pass multiple parameters to a Command

    public class SendCommand : ICommand { public void Execute(object parameter) { var labels = ((object[ ...

  4. A memory leak issue with WPF Command Binding

    Background In our application, we have a screen which hosts several tabs. In each tab, it contains a ...

  5. 管窥MVVMLight Command参数绑定和事件传递

    前言 由于在实际项目中,业务功能的增加导致软件开发规模在逐渐变大,所以我准备找个Silverlight框架来组织当前项目中的文件,以期能够让后续的业务功能增添和维护更加容易一些.无意中,我在这篇文章中 ...

  6. Tkinter教程之Button篇(1)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298 #Tkinter教程之Button篇(1)#Button功能触发事件'''1.一个 ...

  7. Dynamics CRM 2015-Form之添加Ribbon Button

    说到在CRM Form上添加Ribbon Button,那就不得不提到一个Tool:Ribbon Workbench,使用这个Tool,能为我们添加button带来不少便利. Ribbon Workb ...

  8. 从PRISM开始学WPF(六)MVVM(二)Command?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  9. WPF Command

    使用CustomControl时绑定Command用法 C# Part public static RoutedUICommand ClearCommand { get; private set; } ...

随机推荐

  1. SVN教程(包括小乌龟) 全图解

    转载自http://www.cnblogs.com/armyfai/p/3985660.html SVN使用教程总结 SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很 ...

  2. js学习-es6实现枚举

    最近大部分时间再写dart,突然用到js,发现js不能直接声明一个枚举.搜索发现还是有实现的方式,于是总结一下. 目录 枚举特点 Object.freeze() Symbol 实现 体现不可更改 体现 ...

  3. CentOS更换网易yum源

    最新内容和地址参见http://mirrors.163.com/.help/centos.html 1 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yu ...

  4. 在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题

    在docker for windows建立mssql容器后,ssms连接mssql出现错误号码18456的问题 笔者提供一个可能会没考虑到的点. 请检查本机是否安装了mssql!!! 请检查本机的ms ...

  5. 如何进行TIDB优化之Grafana(TiDB 3.0)关注监控指标

    前言 在对数据库进行优化前,我们先要思考一下数据库系统可能存在的瓶颈所在之外.数据库服务是运行在不同的硬件设备上的,优化即通过参数配置(不考虑应用客户端程序的情况下),而实现硬件资源的最大利用化.那么 ...

  6. 【强连通分量】Proving Equivalences

    [题目链接]hdu-2767 [题目描述] Consider the following exercise, found in a generic linear algebra textbook. L ...

  7. xhell、xftp、putty使用教程

    作为远程登陆工具,上传代码登陆服务器工具 1.XSHELL Xshell是远程连接Linux服务器的工具,基于SSH协议,使用它可以更加方便的操作Linux操作系统,在刚使用时可能需要提前简单的设置下 ...

  8. Deepin 20.2.2 /UOS 20.2 添加ppa源

    由于 工作需要,需要通过PPA安装一些优质的软件包,但是 Deepin 默认不支持PPA源 解决方法 由于Deepin/Uos系统默认是没有安装PPA的那么我们得先安装PPA来支持"add- ...

  9. 一文说清OpenCL框架

    背景 Read the fucking official documents! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: 对不 ...

  10. php 获取上个月的起止时间戳

    $thismonth = date('m'); $thisyear = date('Y'); if ($thismonth == 1) { $lastmonth = 12; $lastyear = $ ...