zoukankan      html  css  js  c++  java
  • c# button Command

    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));
    
            }
        }
    
  • 相关阅读:
    svn 回退/更新/取消至某个版本命令详解
    SVN版本回退
    用Visual Studio编辑Linux代码
    vim——打开多个文件、同时显示多个文件、在文件之间切换
    vim下的ctags和taglist等的使用和配置
    Uber优步北京第二、三组奖励政策
    Uber优步北京第一组奖励政策
    uber在限制新司机加入了,看看新政策把
    软件架构 "4+1" 视图模型
    软件体系结构经典问题——KWIC的分析和解决
  • 原文地址:https://www.cnblogs.com/androllen/p/14444667.html
Copyright © 2011-2022 走看看