明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 695|回复: 1

WPF应用Binding之MultiBinding

[复制链接]
发表于 2018-5-8 22:37 | 显示全部楼层 |阅读模式
本文引用地址:https://blog.csdn.net/lh806732/article/details/38679043

有时目标绑定多个源的数据,此时需要使用多路绑定MultiBinding。

本例4个TextBox(textBox1, textBox2, textBox3, textBox4)及一个按钮,当textBox1与textBox2内容相同,并且textBox3与textBox4内容相同时,Button有效。

一、方式一(通过C#进行绑定)

  1. <Grid>  
  2.     <StackPanel>  
  3.         <TextBox x:Name="TextBox1" Margin="5"/>  
  4.         <TextBox x:Name="TextBox2" Margin="5"/>  
  5.         <TextBox x:Name="TextBox3" Margin="5"/>  
  6.         <TextBox x:Name="TextBox4" Margin="5"/>  
  7.         <Button x:Name="btn_Commit" Content="Commit" Margin="5" Click="btn_Commit_Click" />  
  8.     </StackPanel>  
  9. </Grid>  
复制代码

2. C#逻辑

  1. public partial class MainWindow : Window  
  2.     {  
  3.         public MainWindow()  
  4.         {  
  5.             InitializeComponent();  
  6.   
  7.             SetMultiBinding();  
  8.         }  
  9.   
  10.         private void btn_Commit_Click(object sender, RoutedEventArgs e)  
  11.         {  
  12.   
  13.         }  
  14.   
  15.         private void SetMultiBinding()  
  16.         {  
  17.             Binding b1 = new Binding("Text") { Source = TextBox1 };  //注意此处数据源是TextBox对象,路径是Text属性;
  18.             Binding b2 = new Binding("Text") { Source = TextBox2 };  
  19.             Binding b3 = new Binding("Text") { Source = TextBox3 };  
  20.             Binding b4 = new Binding("Text") { Source = TextBox4 };  
  21.   
  22.             MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };  
  23.             /* MultiBinding对Add顺序敏感 */  
  24.             mb.Bindings.Add(b1);  
  25.             mb.Bindings.Add(b2);  
  26.             mb.Bindings.Add(b3);  
  27.             mb.Bindings.Add(b4);  
  28.   
  29.             mb.Converter = new MultiBindingConvert();  
  30.   
  31.             btn_Commit.SetBinding(Button.IsEnabledProperty, mb);  
  32.         }  
  33.     }  
  34.   
  35.     /* 多路绑定数据转换 */  
  36.     public class MultiBindingConvert : IMultiValueConverter  
  37.     {  
  38.         public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  39.         {  
  40.             if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text))&&  
  41.                 values[0].ToString() == values[1].ToString()                  &&  
  42.                 values[2].ToString() == values[3].ToString()  
  43.                 )  
  44.             {  
  45.                 return true;  
  46.             }  
  47.   
  48.             return false;  
  49.         }  
  50.   
  51.         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
  52.         {  
  53.             throw new NotImplementedException();  
  54.         }  
  55.     }  

二、方式二(通过XAML绑定)

  1. <Window x:Class="WpfApp5.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:WpfApp5"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="198.5" Width="255">
  9.     <Window.Resources>
  10.         <local:LogonButtonEnableConvert x:Key="lbec"/>
  11.     </Window.Resources>

  12.     <Grid>
  13.         <StackPanel>
  14.             <TextBox x:Name="TextBox1" Margin="5"/>
  15.             <TextBox x:Name="TextBox2" Margin="5"/>
  16.             <TextBox x:Name="TextBox3" Margin="5"/>
  17.             <TextBox x:Name="TextBox4" Margin="5"/>
  18.             <Button x:Name="btn_Commit" Content="Commit" Margin="5" Click="btn_Commit_Click">
  19.                 <Button.IsEnabled>
  20.                     <MultiBinding Converter="{StaticResource lbec}" ConverterParameter="ParamTest">
  21.                         <Binding ElementName="TextBox1" Path="Text"/>
  22.                         <Binding ElementName="TextBox2" Path="Text"/>
  23.                         <Binding ElementName="TextBox3" Path="Text"/>
  24.                         <Binding ElementName="TextBox4" Path="Text"/>
  25.                     </MultiBinding>
  26.                 </Button.IsEnabled>
  27.             </Button>
  28.         </StackPanel>
  29.     </Grid>
  30. </Window>

2、C#数据转换类

  1. public class LogonButtonEnableConvert : IMultiValueConverter  
  2. {  
  3.     /*
  4.      * object[] values                          : 所绑定的源的值
  5.      * Type targetType                          : 目标的类型
  6.      * object parameter                         : 绑定时所传递的参数
  7.      * System.Globalization.CultureInfo culture : 系统语言等信息
  8.      */  
  9.     public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
  10.     {  
  11.         if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text))&&  
  12.             values[0].ToString() == values[1].ToString()                  &&  
  13.             values[2].ToString() == values[3].ToString()  
  14.             )  
  15.         {  
  16.             return true;  
  17.         }  
  18.   
  19.         return false;  
  20.     }  
  21.   
  22.     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)  
  23.     {  
  24.         throw new NotImplementedException();  
  25.     }  
  26. }  
复制代码





发表于 2018-5-9 07:56 | 显示全部楼层
小博666……紫薯布丁
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-20 07:28 , Processed in 0.195724 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表