<Window x:Class="TestOfMultiBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <StackPanel Background="LightBlue" > <TextBox x:Name="textBox1" Height="23" Margin="5" /> <TextBox x:Name="textBox2" Height="23" Margin="5,0" /> <TextBox x:Name="textBox3" Height="23" Margin="5" /> <TextBox x:Name="textBox4" Height="23" Margin="5,0" /> <Button x:Name="button1" Content="Submit" Width="80" Margin="5" /> </StackPanel> </Window>
using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; namespace TestOfMultiBinding { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.SetMultiBinding(); } private void SetMultiBinding() { Binding b1 = new Binding("Text") { Source = this.textBox1 }; Binding b2 = new Binding("Text") { Source = this.textBox2 }; Binding b3 = new Binding("Text") { Source = this.textBox3 }; Binding b4 = new Binding("Text") { Source = this.textBox4 }; MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay }; mb.Bindings.Add(b1); mb.Bindings.Add(b2); mb.Bindings.Add(b3); mb.Bindings.Add(b4); mb.Converter = new LogonMultiBindingConverter(); this.button1.SetBinding(Button.IsEnabledProperty, mb); } } public class LogonMultiBindingConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text)) && values[0].ToString() == values[1].ToString() && values[2].ToString() == values[3].ToString()) { return true; } return false; } //不会被调用 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }