如何在 WPF 上使用 ICommand 中的 CanExecute 方法 [英] How to use the CanExecute Method from ICommand on WPF

查看:26
本文介绍了如何在 WPF 上使用 ICommand 中的 CanExecute 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 ICommand 接口中的 CanExecute 方法?

How does one use the CanExecute Method from the ICommand interface?

在我的示例中,我有一个 SaveCommand,它只在对象可保存时启用.我的保存按钮的 XAML 代码如下所示:

In my example i have a SaveCommand which i only what to be enable when the object is saveable. The XAML Code of my Savebutton looks like this:

<Button Content="Save" Command="{Binding SaveCommand, Mode=TwoWay}" />

这是我保存类的代码:

class Save : ICommand
{
    public MainWindowViewModel viewModel { get; set; }

    public Save(MainWindowViewModel viewModel)
    {
        this.viewModel = viewModel;
    }

    public bool CanExecute(object parameter)
    {

        if (viewModel.IsSaveable == false)
            return false;
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        viewModel.Save();
    }
}

ViewModel 中的 save 属性如下所示:

The save property in the ViewModel looks like this:

    public ICommand SaveCommand
    {
        get
        {
            saveCommand = new Save(this);
            return saveCommand;
        }
        set
        {
            saveCommand = value;
        }
    }

这个构造不起作用.当 isSaveable 为 true 时,按钮不会启用自身.

This construct didn't work. The button does not enable its self when isSaveable is true.

推荐答案

不要定义自己的 ICommand 实现,而是使用 RelayCommand.

Instead of defining your own implementation of ICommand, use a RelayCommand.

在下面的示例代码中,当用户在 TextBox 中输入内容时,保存 Button 被启用.

In the below sample code, the save Button is enabled when the user types something in the TextBox.

XAML:

<Window x:Class="RelayCommandDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel HorizontalAlignment="Center">
        <TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="5" Width="120"/>
        <Button Content="Save" Command="{Binding SaveCommand}" Margin="3"/>
    </StackPanel>
</Window>

背后的代码:

using System;
using System.Windows;
using System.Windows.Input;

namespace RelayCommandDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new VM();
        }
    }
    public class VM
    {
        public String Name { get; set; }

        private ICommand _SaveCommand;

        public ICommand SaveCommand
        {
            get { return _SaveCommand; }
        }

        public VM()
        {
            _SaveCommand = new RelayCommand(SaveCommand_Execute, SaveCommand_CanExecute);
        }

        public void SaveCommand_Execute()
        {
            MessageBox.Show("Save Called");
        }

        public bool SaveCommand_CanExecute()
        {
            if (string.IsNullOrEmpty(Name))
                return false;
            else
                return true;
        }
    }

    public class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        private Action methodToExecute;
        private Func<bool> canExecuteEvaluator;
        public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
        {
            this.methodToExecute = methodToExecute;
            this.canExecuteEvaluator = canExecuteEvaluator;
        }
        public RelayCommand(Action methodToExecute)
            : this(methodToExecute, null)
        {
        }
        public bool CanExecute(object parameter)
        {
            if (this.canExecuteEvaluator == null)
            {
                return true;
            }
            else
            {
                bool result = this.canExecuteEvaluator.Invoke();
                return result;
            }
        }
        public void Execute(object parameter)
        {
            this.methodToExecute.Invoke();
        }
    }
}

这篇关于如何在 WPF 上使用 ICommand 中的 CanExecute 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆