• 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));
    
            }
        }
    
  • 相关阅读:
    自动化测试面试题及答案
    Jmeter读取CSV数据显示EOF问题
    C++中类继承public,protected和private关键字作用详解及派生类的访问权限
    汇编实验:寻址方式在数据访问中的应用
    集成运放综合应用的仿真模拟
    集成运算放大电路线性区运用的仿真实验
    共射/共源放大电路的仿真实验
    二极管特性的仿真实验
    各主流排序算法详细介绍(时间/空间复杂度,适用范围和稳定性)
    三大自由之二 部分
  • 原文地址:https://www.cnblogs.com/androllen/p/14444667.html
Copyright © 2020-2023  润新知