C# WPF 命令机制(关闭CanExecute自动触发,改手动)

在绑定数据变化时手动触发UI状态检查

xml 复制代码
<Window x:Class="WpfApp5.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:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button Content="点击"  
                Command="{Binding ClickCommand}"  
                Margin="10" Padding="15" />
        <Button Content="Sub"  
        Command="{Binding ClickCommandSub}"  
        Margin="10" Padding="15" />
        <TextBlock Text="{Binding Count, StringFormat='已点击次数: {0}'}"  
                   HorizontalAlignment="Center" Width="80" Margin="10"/>
    </StackPanel>
</Window>
csharp 复制代码
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }
}



public class ManualRelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Func<object, bool> _canExecute;

    public ManualRelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter) => _canExecute?.Invoke(parameter) ?? true;
    public void Execute(object parameter) => _execute(parameter);

    // 手动触发事件  
    public event EventHandler CanExecuteChanged;
    public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

    //public event EventHandler CanExecuteChanged
    //{
    //    add => CommandManager.RequerySuggested += value;
    //    remove => CommandManager.RequerySuggested -= value;
    //}

     同时保留手动触发方法  
    //public void ForceRaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
}

public class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


    private int _count;
    public int Count
    {
        get => _count;
        set
        {
            if (_count == value) return;
            _count = value;
            OnPropertyChanged();
            // 手动触发命令状态更新  
            ClickCommand.RaiseCanExecuteChanged();
        }
    }

    public ManualRelayCommand ClickCommand { get; }
    public ICommand ClickCommandSub
    {
        get { return new ManualRelayCommand(_ => Count--, _ => true); }
        set { }
    }

    public MainViewModel()
    {
        ClickCommand = new ManualRelayCommand(
            ClickEvent,
           _CanExecute 
        );
    }
    private void ClickEvent(object obj)
    {
        Count++;
    }

    private bool _CanExecute(object arg)
    {
        return Count < 3;
    }
}
相关推荐
小码编匠7 小时前
WPF 中的高级交互通过右键拖动实现图像灵活缩放
后端·c#·.net
唐青枫14 小时前
C#.NET 定时任务与队列利器:Hangfire 完整教程
c#·.net
hez201020 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools1 天前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫2 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫3 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务3 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther3 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎