WPF Button 的IsEnable绑定到多个属性

首先看效果

WPF Button 的IsEnable绑定到多个属性

准备一个ViewModel

 1 public class TestViewModel : ViewModelBase
 2     {
 3         private double _argA;
 4 
 5         private double _argB;
 6 
 7         private double _result;
 8 
 9         public RelayCommand<AddObject> AddCmd { get; set; }
10 
11         public double ArgA
12         {
13             get => _argA;
14             set => Set(value, ref _argA);
15         }
16 
17         public double ArgB
18         {
19             get => _argB;
20             set => Set(value, ref _argB);
21         }
22 
23         public double Result
24         {
25             get => _result;
26             set => Set(value, ref _result);
27         }
28 
29         public TestViewModel()
30         {
31             AddCmd = new RelayCommand<AddObject>(Add);
32         }
33 
34         private void Add(AddObject addObject)
35         {
36             Result = addObject.ArgA + addObject.ArgB;
37         }
38     }

准备一个数据传输类

1 public class AddObject
2     {
3         public double ArgA { get; set; }
4 
5         public double ArgB { get; set; }
6     }

准备一个多值转换类 

 1 public class MultiBindingConverter : IMultiValueConverter
 2     {
 3         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
 4         {
 5             var addObject = new AddObject();
 6             var list = new List<double>();
 7             for (int i = 0; i < values.Length; i++)
 8             {
 9                 //如果返回失败,直接结束
10                 var flag = double.TryParse(values[i].ToString(), out double m);
11                 if (!flag)
12                 {
13                     return null;
14                 }
15                 list.Add(m);
16             }
17 
18             addObject.ArgA = list[0];
19             addObject.ArgB = list[1];
20             return addObject;
21         }
22 
23         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
24         {
25             throw new NotImplementedException();
26         }
27     }

准备一个多值转换类 控制Button的是否可用

 1 public class EnableConverter : IMultiValueConverter
 2     {
 3         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
 4         {
 5             //输入值能否转换为数字
 6             return values.All(v => double.TryParse(v.ToString(), out _));
 7         }
 8 
 9         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
10         {
11             throw new NotImplementedException();
12         }
13     }

前台界面

 1 <Window
 2     x:Class="WpfApp1.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:WpfApp1"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     Title="MainWindow"
 9     Width="800"
10     Height="450"
11     FontSize="20"
12     mc:Ignorable="d">
13     <Window.Resources>
14         <local:MultiBindingConverter x:Key="converter" />
15         <local:EnableConverter x:Key="enableConverter" />
16     </Window.Resources>
17     <Window.DataContext>
18         <local:TestViewModel />
19     </Window.DataContext>
20     <Grid>
21         <StackPanel
22             Width="400"
23             HorizontalAlignment="Center"
24             VerticalAlignment="Center"
25             Orientation="Vertical">
26             <StackPanel Orientation="Horizontal">
27                 <TextBlock Margin="5" Text="参数A" />
28                 <TextBox
29                     x:Name="argA"
30                     MinWidth="100"
31                     Margin="5"
32                     Text="{Binding ArgA}" />
33             </StackPanel>
34             <StackPanel Orientation="Horizontal">
35                 <TextBlock Margin="5" Text="参数B" />
36                 <TextBox
37                     x:Name="argB"
38                     MinWidth="100"
39                     Margin="5"
40                     Text="{Binding ArgB}" />
41             </StackPanel>
42             <StackPanel Orientation="Horizontal">
43                 <TextBlock Margin="5" Text="结果:" />
44                 <TextBlock
45                     MinWidth="100"
46                     Margin="5"
47                     Text="{Binding Result}" />
48                 <Button
49                     MinWidth="100"
50                     Margin="5"
51                     Command="{Binding AddCmd}"
52                     Content="计算">
53                     <Button.IsEnabled>
54                         <MultiBinding Converter="{StaticResource enableConverter}">
55                             <Binding ElementName="argA" Path="Text" />
56                             <Binding ElementName="argB" Path="Text" />
57                         </MultiBinding>
58                     </Button.IsEnabled>
59                     <Button.CommandParameter>
60                         <MultiBinding Converter="{StaticResource converter}">
61                             <Binding ElementName="argA" Path="Text" />
62                             <Binding ElementName="argB" Path="Text" />
63                         </MultiBinding>
64                     </Button.CommandParameter>
65                 </Button>
66             </StackPanel>
67         </StackPanel>
68     </Grid>
69 </Window>

 

这个是否可用,应该是配合数据校验来使用 比如IDataErrorInfos ValidationRule等。

 

WPF Button 的IsEnable绑定到多个属性

上一篇:与众不同 windows phone (5) - Chooser(选择器)


下一篇:wpf实现轮播效果