在wpf中,绑定(Binding)是一个重要的概念,建立数据源和目标之间的数据传输,可以实时的反馈源和目标的数据变化。
多重绑定(MultiBinding)是绑定的扩展。为了区别起见,通常使用的Binding是指单重绑定,数据源只有一个,与之相对应,多重绑定的数据源有多个。
举个简单的例子来说明多重绑定的使用方法:设计一个UI界面,只有两个CheckBox控件,一个控件内容为“四边形”,一个控件内容为“内角相等”,只有两个控件都是选中的状态下才打印“矩形”的提示。
为了便于说明,现在直接使用C#源码来实现这个逻辑。
XAML的部分如下:
<Window x:Class="MultiBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="多重绑定" Height="200" Width="300">
<Grid>
<CheckBox x:Name="cond1" HorizontalAlignment="Left" Margin="110,45,0,0" VerticalAlignment="Top" Content="四边形" Width="68" FontFamily="Microsoft YaHei" Click="onClicked"/>
<CheckBox x:Name="cond2" HorizontalAlignment="Left" Margin="110,95,0,0" VerticalAlignment="Top" Content="内角相等" Width="68" FontFamily="Microsoft YaHei" Click="onClicked"/>
</Grid>
</Window>
在MainWindow.xaml.cs中的源码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
namespace MultiBindingTest
{
public class ConverterClass : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] != null && values[1] != null)
return (bool)values[0] && (bool)values[1];
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static readonly DependencyProperty IsRectangleProperty
= DependencyProperty.Register("IsRectangle", typeof(bool), typeof(MainWindow));
bool IsRectangle
{
get
{
return (bool)GetValue(IsRectangleProperty);
}
set
{
SetValue(IsRectangleProperty, value);
}
}
public MainWindow()
{
InitializeComponent();
MultiBinding mb = new MultiBinding() { Mode = BindingMode.OneWay };
Binding b1 = new Binding("IsChecked") { Source = cond1 };
Binding b2 = new Binding("IsChecked") { Source = cond2 };
mb.Bindings.Add(b1); mb.Bindings.Add(b2);
mb.Converter = new ConverterClass(); SetBinding(IsRectangleProperty, mb);
}
private void onClicked(object sender, RoutedEventArgs e)
{
if (IsRectangle)
MessageBox.Show("矩形");
}
}
}
</pre><p></p><pre>
这里定义了一个依赖属性IsRectangleProperty,通过属性IsRectangle进行一层封装。同时选中两个checkbox时,通过转换器ConverterClass的转义,获得的逻辑值为True,赋给依赖属性IsRectangleProperty,之后访问它的对外属性IsRectangle,就可以得到多重绑定的数据源复合的结果了。
需要注意的是,如果删除onClicked()方法,只通过IsRectangle的实现能否实现呢?
bool IsRectangle
{
get
{
return (bool)GetValue(IsRectangleProperty);
}
set
{
if (value)
MessageBox.Show("矩形");
SetValue(IsRectangleProperty, value);
}
}
以上代码意图通过截取对IsRectangle的赋值来弹出消息框,想法是不错的,但是达不到提示目的。因为IsRectangle虽然是依赖属性的封装,但同样是属性,只有直接访问它,才会触发set/get语句。点击checkbox,虽然会改变IsRectangle的值,但是并不经过set/get语句实现,而是通过依赖属性内部一套复杂的机制完成的。感兴趣的同学可以参考刘铁锰写的《深入浅出WPF》的第七张的7.2.3节。
通过c#代码可以从本质上了解多重绑定的实现方法,当然XAML同样可以达到目的。
<Window x:Class="MultiBindingTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiBindingTest"
Title="多重绑定" Height="200" Width="300">
<Window.Resources>
<local:ConverterClass x:Key="multibinding"/>
</Window.Resources>
<Grid>
<CheckBox x:Name="cond1" HorizontalAlignment="Left" Margin="110,45,0,0" VerticalAlignment="Top" Content="四边形" Width="68" FontFamily="Microsoft YaHei"/>
<CheckBox x:Name="cond2" HorizontalAlignment="Left" Margin="110,76,0,0" VerticalAlignment="Top" Content="内角相等" Width="68" FontFamily="Microsoft YaHei"/>
<Label Content="矩形" HorizontalAlignment="Left" Margin="110,108,0,0" VerticalAlignment="Top" Width="68" FontFamily="Microsoft YaHei" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
<Label.IsEnabled>
<MultiBinding Converter="{StaticResource multibinding}">
<Binding Path="IsChecked" ElementName="cond1"/>
<Binding Path="IsChecked" ElementName="cond2"/>
</MultiBinding>
</Label.IsEnabled>
</Label>
</Grid>
</Window>
这里保留了转换器类ConverterClass,并且引入了Label元素。运行结果如下: