WPF应用Binding之MultiBinding
本文引用地址:https://blog.csdn.net/lh806732/article/details/38679043有时目标绑定多个源的数据,此时需要使用多路绑定MultiBinding。本例4个TextBox(textBox1, textBox2, textBox3, textBox4)及一个按钮,当textBox1与textBox2内容相同,并且textBox3与textBox4内容相同时,Button有效。一、方式一(通过C#进行绑定)<Grid>
<StackPanel>
<TextBox x:Name="TextBox1" Margin="5"/>
<TextBox x:Name="TextBox2" Margin="5"/>
<TextBox x:Name="TextBox3" Margin="5"/>
<TextBox x:Name="TextBox4" Margin="5"/>
<Button x:Name="btn_Commit" Content="Commit" Margin="5" Click="btn_Commit_Click" />
</StackPanel>
</Grid>2. C#逻辑public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SetMultiBinding();
}
private void btn_Commit_Click(object sender, RoutedEventArgs e)
{
}
private void SetMultiBinding()
{
Binding b1 = new Binding("Text") { Source = TextBox1 };//注意此处数据源是TextBox对象,路径是Text属性;
Binding b2 = new Binding("Text") { Source = TextBox2 };
Binding b3 = new Binding("Text") { Source = TextBox3 };
Binding b4 = new Binding("Text") { Source = TextBox4 };
MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };
/* MultiBinding对Add顺序敏感 */
mb.Bindings.Add(b1);
mb.Bindings.Add(b2);
mb.Bindings.Add(b3);
mb.Bindings.Add(b4);
mb.Converter = new MultiBindingConvert();
btn_Commit.SetBinding(Button.IsEnabledProperty, mb);
}
}
/* 多路绑定数据转换 */
public class MultiBindingConvert : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text))&&
values.ToString() == values.ToString() &&
values.ToString() == values.ToString()
)
{
return true;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}二、方式二(通过XAML绑定)<Window x:Class="WpfApp5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="198.5" Width="255">
<Window.Resources>
<local:LogonButtonEnableConvert x:Key="lbec"/>
</Window.Resources>
<Grid>
<StackPanel>
<TextBox x:Name="TextBox1" Margin="5"/>
<TextBox x:Name="TextBox2" Margin="5"/>
<TextBox x:Name="TextBox3" Margin="5"/>
<TextBox x:Name="TextBox4" Margin="5"/>
<Button x:Name="btn_Commit" Content="Commit" Margin="5" Click="btn_Commit_Click">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource lbec}" ConverterParameter="ParamTest">
<Binding ElementName="TextBox1" Path="Text"/>
<Binding ElementName="TextBox2" Path="Text"/>
<Binding ElementName="TextBox3" Path="Text"/>
<Binding ElementName="TextBox4" Path="Text"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
</StackPanel>
</Grid>
</Window>2、C#数据转换类public class LogonButtonEnableConvert : IMultiValueConverter
{
/*
* object[] values : 所绑定的源的值
* Type targetType : 目标的类型
* object parameter : 绑定时所传递的参数
* System.Globalization.CultureInfo culture : 系统语言等信息
*/
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!values.Cast<string>().Any(text => string.IsNullOrEmpty(text))&&
values.ToString() == values.ToString() &&
values.ToString() == values.ToString()
)
{
return true;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
小博666……紫薯布丁
页:
[1]