WPF MVVMToolkit框架学习 命令的CanExecute

演示一下:如果文本框有内容,按钮就可用,如果没有内容,按钮就禁用

 

后台代码

 1 using Microsoft.Toolkit.Mvvm.ComponentModel;
 2 using Microsoft.Toolkit.Mvvm.Input;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using System.Windows;
 9 
10 namespace MVVMToolkit框架学习.ViewModels
11 {
12     internal class TestViewModel : ObservableObject
13     {
14         private string _name = string.Empty;
15 
16         private IRelayCommand _submitCommand = null;
17 
18         public string Name
19         {
20             get
21             {
22                 return _name;
23             }
24 
25             set
26             {
27                 SetProperty(ref _name, value);
28                 //触发验证事件 可以写在全局验证中
29                 SumbmitCommand.NotifyCanExecuteChanged();
30             }
31         }
32 
33         public IRelayCommand SumbmitCommand
34         {
35             get
36             {
37                 return _submitCommand;
38             }
39 
40             set
41             {
42                 _submitCommand = value;
43             }
44         }
45 
46         public TestViewModel()
47         {
48             SumbmitCommand = new RelayCommand(ShowYourInput, () =>
49             {
50                 //验证逻辑
51                 return !string.IsNullOrWhiteSpace(Name);
52             });
53         }
54 
55         /// <summary>
56         /// 
57         /// </summary>
58         private void ShowYourInput()
59         {
60             MessageBox.Show("你的输入:" + Name, "信息");
61         }
62     }
63 }

前台界面

 1 <Window
 2     x:Class="MVVMToolkit框架学习.MainWindow"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:MVVMToolkit框架学习"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     Title="MainWindow"
 9     Width="800"
10     Height="450"
11     mc:Ignorable="d">
12     <Grid>
13         <StackPanel
14             VerticalAlignment="Center"
15             Orientation="Vertical">
16             <StackPanel
17                 HorizontalAlignment="Center"
18                 VerticalAlignment="Center"
19                 Orientation="Horizontal">
20                 <TextBlock
21                     VerticalAlignment="Center"
22                     Text="请输入名字:" />
23                 <!--在每一次属性更改的时候都验证 也可在LostFocus的时候验证-->
24                 <TextBox
25                     Width="250"
26                     VerticalContentAlignment="Center"
27                     BorderBrush="Black"
28                     Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Cursor="IBeam" />
29                 
30             </StackPanel>
31             <Button
32                 Width="80"
33                 Margin="0,10,0,0"
34                 Command="{Binding SumbmitCommand}"
35                 CommandParameter="{Binding ElementName=parameter, Path=Text}"
36                 Content="提交" Cursor="Hand" />
37         </StackPanel>
38     </Grid>
39 </Window>

 

 

posted @ 2021-12-22 13:20  只吃肉不喝酒  阅读(687)  评论(0编辑  收藏  举报