c# – 如何在WPF上使用ICommand的CanExecute方法

前端之家收集整理的这篇文章主要介绍了c# – 如何在WPF上使用ICommand的CanExecute方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先要说的是我在 WPF和MVVM模式的最开始.

在尝试一些自定义命令时,我想知道如何使用ICommand接口中的CanExecute Methode.

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

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

这是我的保存类的代码

  1. class Save : ICommand
  2. {
  3. public MainWindowviewmodel viewmodel { get; set; }
  4.  
  5. public Save(MainWindowviewmodel viewmodel)
  6. {
  7. this.viewmodel = viewmodel;
  8. }
  9.  
  10. public bool CanExecute(object parameter)
  11. {
  12.  
  13. if (viewmodel.IsSaveable == false)
  14. return false;
  15. return true;
  16. }
  17.  
  18. public event EventHandler CanExecuteChanged;
  19.  
  20. public void Execute(object parameter)
  21. {
  22. viewmodel.Save();
  23. }
  24. }

viewmodel中的save属性如下所示:

  1. public ICommand SaveCommand
  2. {
  3. get
  4. {
  5. saveCommand = new Save(this);
  6. return saveCommand;
  7. }
  8. set
  9. {
  10. saveCommand = value;
  11. }
  12. }

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

解决方法

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

在下面的示例代码中,当用户在TextBox中键入内容时,将启用保存按钮.

XAML:

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

代码背后:

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4.  
  5. namespace RelayCommandDemo
  6. {
  7. public partial class MainWindow : Window
  8. {
  9. public MainWindow()
  10. {
  11. InitializeComponent();
  12.  
  13. DataContext = new VM();
  14. }
  15. }
  16. public class VM
  17. {
  18. public String Name { get; set; }
  19.  
  20. private ICommand _SaveCommand;
  21.  
  22. public ICommand SaveCommand
  23. {
  24. get { return _SaveCommand; }
  25. }
  26.  
  27. public VM()
  28. {
  29. _SaveCommand = new RelayCommand(SaveCommand_Execute,SaveCommand_CanExecute);
  30. }
  31.  
  32. public void SaveCommand_Execute()
  33. {
  34. MessageBox.Show("Save Called");
  35. }
  36.  
  37. public bool SaveCommand_CanExecute()
  38. {
  39. if (string.IsNullOrEmpty(Name))
  40. return false;
  41. else
  42. return true;
  43. }
  44. }
  45.  
  46. public class RelayCommand : ICommand
  47. {
  48. public event EventHandler CanExecuteChanged
  49. {
  50. add { CommandManager.RequerySuggested += value; }
  51. remove { CommandManager.RequerySuggested -= value; }
  52. }
  53. private Action methodToExecute;
  54. private Func<bool> canExecuteEvaluator;
  55. public RelayCommand(Action methodToExecute,Func<bool> canExecuteEvaluator)
  56. {
  57. this.methodToExecute = methodToExecute;
  58. this.canExecuteEvaluator = canExecuteEvaluator;
  59. }
  60. public RelayCommand(Action methodToExecute)
  61. : this(methodToExecute,null)
  62. {
  63. }
  64. public bool CanExecute(object parameter)
  65. {
  66. if (this.canExecuteEvaluator == null)
  67. {
  68. return true;
  69. }
  70. else
  71. {
  72. bool result = this.canExecuteEvaluator.Invoke();
  73. return result;
  74. }
  75. }
  76. public void Execute(object parameter)
  77. {
  78. this.methodToExecute.Invoke();
  79. }
  80. }
  81. }

猜你在找的C#相关文章