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中实现。

WPF实现多值绑定特性以及多值转换的更多相关文章

  1. 总结:WPF中MultiBinding多值绑定的方法

    原文:总结:WPF中MultiBinding多值绑定的方法 一.Xaml中绑定代码: <TextBlock  Grid.Row="5" Grid.Column="3 ...

  2. WPF多值绑定及多值转换(MultiBinding和IMultiValueConverter)

    WPF可以使用MultiBinding进行多值绑定,使用IMultiValueConverter进行多值转换 例: (1)转换器 public class ContentConverter : IMu ...

  3. WPF中DatePiker值绑定以及精简查询

    WPF中DatePiker值绑定以及精简查询 1.WPF中DatePiker值绑定 Xaml中值绑定使用Text <DatePicker Text="{Binding strMinDa ...

  4. [WPF 基础知识系列] —— 绑定中的数据校验Vaildation

    前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...

  5. wpf依赖属性、绑定实现原理、附加属性学习

    依赖属性和普通属性相比节省内存的原因:对于普通属性,每个对象有需要存储一个普通属性的值,即便是默认值.而依赖属性的默认值是静态的存储在类中的,所有对象都使用同一默认值,所以对于拥有大量属性的控件来说这 ...

  6. 利用WPF创建含多种交互特性的无边框窗体

    咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...

  7. C++11新特性:右值引用和转移构造函数

    问题背景 #include <iostream> using namespace std; vector<int> doubleValues (const vector< ...

  8. WPF中,如何将绑定源设置到单件实例

    原文:WPF中,如何将绑定源设置到单件实例  WPF中,如何将绑定源设置到单件实例                                       周银辉 大概两个月前,曾有位朋友问我:如 ...

  9. WPF——TargetNullValue(如何在绑定空值显示默认字符)

    原文:WPF--TargetNullValue(如何在绑定空值显示默认字符) 说明:在数据绑定时,如果有些字段为空值,那么在数据绑定时可以用默认值来显示为空的字段. </Grid> { L ...

随机推荐

  1. 第三方框架ViewPagerIndicator引入到Android Studio的方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6286619.html 第三方框架ViewPagerIndicator实现的效果比较好,但当我们从G ...

  2. nginx的RTMP协议服务器

    nginx的RTMP协议服务器 by ahuner 通过以下的配置,可以使nginx接收RTMP流,并在web上播放实时视频. 1.openssl安装 nginx需要http_ssl_module模块 ...

  3. 单源最短路-dijkstra算法(未优化)

    bool used[maxn]; int g[maxn][maxn]; // 边未联系的填充为INF int d[maxn]; void dijkstra(int s){ memset(g,false ...

  4. CodeForces 606A Magic Spheres

    水题 /* *********************************************** Author :Zhou Zhentao Email :774388357@qq.com C ...

  5. iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)

    iOS传值方式之5:通知传值 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter ...

  6. ionic系列

    1. 推荐中文API https://github.com/ychow/ionic-guide

  7. Ubuntu如何备份和恢复系统 - 落花往事的日志 - 网易博客

    在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过Windows系统,那么你一定记忆犹新:首先需要找到一个备份工 具(通常都是私有软件),然后重启电脑进入备份工具提供 ...

  8. 【亲测】Python:解决方案:Python Version 2.7 required, which was not found in the registry

    好久不更新随笔了,今天因为数据可视化作业,想抓取一些人人网好友关系数据,于是开始尝试python,用到numpy模块,安装的时候提示: 'Python Version 2.7 required, wh ...

  9. html中的图片、css、js等路径加载问题

    网页文件的存取路径有3种:物理路径.绝对路径和相对路径. 物理路径就是你的文件放在主机上的具体位置,例如:D:\\image\\1.jpg 这种格式,该方法可以很快确定出你的文件,但是在网页显示路径基 ...

  10. FIFO存储器

    FIFO( First In First Out)简单说就是指先进先出.由于微电子技术的飞速发展,新一代FIFO芯片容量越来越大,体积越来越小,价格越来越便宜.作为一种新型大规模集成电路,FIFO芯片 ...