如何在WPF中使用ICommand的CanExecute方法

11

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


在我的示例中,我有一个SaveCommand,我只想在对象可保存时启用它。我的Savebutton的XAML代码如下:

<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 属性看起来像这样:

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

这个结构体不起作用了。当isSaveable为真时,按钮没有启用。


1
绑定命令时是否需要双向绑定? - Peter Bruins
没想到我会通过将整个视图模型传递给命令而得逞(现在我不知道是什么给了我这个想法!)。尽管与我的问题无关,但在此处看到的示例代码挽救了我 UI 设计中的一个关键部分,使其免于被废弃。 - Ger
1个回答

11

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

在以下示例代码中,当用户在 TextBox 中输入内容时,保存 Button 会被启用。

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();
        }
    }
}

29
为什么建议使用RelayCommand - Soleil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接