如何使用WPF上的ICommand使用CANEXECUTE方法



如何使用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中的保存属性看起来像:

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

这个结构不起作用。该按钮不会在可见的时候使自己自我。

,而不是定义您自己的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();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新