• 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));
    
            }
        }
    
  • 相关阅读:
    C++ 如何判断所调用的重载函数
    C++ 入门5 类和动态内存分配(一)
    c#动态创建ODBC数据源
    设为首页,加入收藏,联系我们
    ASP.NET 2.0中CSS失效
    typedef的四个用途和两个陷阱(转)
    VC++实现应用程序对插件的支持(转)
    DOM无关事件
    How to Migrate from WCF Web API to ASP.NET Web API
    Using ASP.NET Web API with ASP.NET Web Forms
  • 原文地址:https://www.cnblogs.com/androllen/p/14444667.html
Copyright © 2020-2023  润新知