阅读 111

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 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(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     
14         "converter" />
15         "enableConverter" />
16     
17     
18         
19     
20     
21         <StackPanel
22             Width="400"
23             HorizontalAlignment="Center"
24             VerticalAlignment="Center"
25             Orientation="Vertical">
26             "Horizontal">
27                 "5" Text="参数A" />
28                 <TextBox
29                     x:Name="argA"
30                     MinWidth="100"
31                     Margin="5"
32                     Text="{Binding ArgA}" />
33             
34             "Horizontal">
35                 "5" Text="参数B" />
36                 <TextBox
37                     x:Name="argB"
38                     MinWidth="100"
39                     Margin="5"
40                     Text="{Binding ArgB}" />
41             
42             "Horizontal">
43                 "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                     
54                         "{StaticResource enableConverter}">
55                             "argA" Path="Text" />
56                             "argB" Path="Text" />
57                         
58                     
59                     
60                         "{StaticResource converter}">
61                             "argA" Path="Text" />
62                             "argB" Path="Text" />
63                         
64                     
65                 
66             
67         
68     
69 

 

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

 

原文:https://www.cnblogs.com/AtTheMoment/p/15008812.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐