WPF中的实现
我们首先来看一下常规的绑定
<Window x:Class="WpfApplicationSample.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"> <Grid> <!--WPF 单值绑定--> <TextBlock Text="{Binding Title}"></TextBlock> </Grid></Window>
这个很简单,我们几乎不需要做任何解释
接下来看一下WPF中如何进行多值绑定
<Window x:Class="WpfApplicationSample.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"> <Grid> <!--WPF 单值绑定--> <TextBlock Text="{Binding Title}"></TextBlock> <!--WPF 多值绑定,结合StringFormat--> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=" {0}-{1}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid></Window>
这是第一种多值绑定方式,可以直接通过StringFormat格式化多个值,并最终显示在TextBlock中。这种做法,在很多时候,都够用了。
但是,在某些时候,我们可能需要对这些多个值做复杂的处理,光用StringFormat满足不了要求,怎么办呢?
是的,我们会联想到使用ValueConverter。在System.Windows.Data这个命名空间中,我们以前用过一个IValueConverter的接口对吧,那是针对单值绑定的。关于这个接口,更多信息,可以参考 http://msdn.microsoft.com/zh-cn/library/system.windows.data.ivalueconverter.aspx
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;namespace WpfApplicationSample{ /// <summary> /// WPF单值绑定转换器 /// 作者:陈希章 /// </summary> public class TitleConverter:IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //这里实现具体逻辑 throw new NotImplementedException(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //这里实现具体逻辑 throw new NotImplementedException(); } #endregion }}
既然是这个思路,那么有没有多值转换器呢?答案是有的。请参考
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Data;namespace WpfApplicationSample{ /// <summary> /// WPF单值绑定转换器 /// 作者:陈希章 /// </summary> class MultiValueConverterSample:IMultiValueConverter { #region IMultiValueConverter Members public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { //这里实现具体逻辑,请注意第一个参数是一个数组,可以传递多个值 throw new NotImplementedException(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }}
那么,如何在XAML中使用这个转换器呢?其实和单值转换器是一样的,请参考下面的语法
<Window x:Class="WpfApplicationSample.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" xmlns:local="clr-namespace:WpfApplicationSample"> <Window.Resources> <local:MultiValueConverterSample x:Key="cv"></local:MultiValueConverterSample> </Window.Resources> <Grid> <!--WPF 单值绑定--> <TextBlock Text="{Binding Title}"></TextBlock> <!--WPF 多值绑定,结合StringFormat--> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat=" {0}-{1}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> <!--WPF 多值绑定,结合Converter--> <TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource cv}"> <Binding Path="Title"></Binding> <Binding Path="Time"></Binding> </MultiBinding> </TextBlock.Text> </TextBlock> </Grid></Window>
看起来很好理解,对吧?这是WPF中为我们默认就提供的功能,确实很方便。
但是,这个特性(多值绑定)却没有在Silverlight中实现。