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. 转:web_custom_request应用示例

    LoadRunner提供的web_custom_request函数可以用于实现参数的动态生成.在LoadRunner中,web_reg_save_param和custom_request都常于处理参数 ...

  2. Type safety: Unchecked cast from Object to ArrayList

    表明Object转化为ArrayList这个转化并不是安全的.. 编译的时候需要加入修饰符才能正常编译(具体是那个修饰符..不记得了.^_^),否则会提示有警告 当然这只是一个警告,如果楼主自信这个转 ...

  3. Android的init过程详解(一)

    Android的init过程详解(一) Android的init过程(二):初始化语言(init.rc)解析 本文使用的软件版本 Android:4.2.2 Linux内核:3.1.10 本文及后续几 ...

  4. 写自己的一个pdo数据库操作框架

    http://stackoverflow.com/questions/20669850/pdo-database-abstraction-layer-with-multiple-queries-in- ...

  5. background系列属性

    1.background-color背景颜色属性 ①颜色表示方法 英语单词:red   blue   purple    skyblue. rgb:r代表红色   g代表绿色   b代表蓝色    也 ...

  6. 设计 无状态的类,而不是 stateful

    0down votefavorite   I have created a Database Abstraction Layer over PDO to refrain from creating m ...

  7. compass scss blueprint

    [转载] 今天在执行compass create my-grid –using blueprint 命令时发现报错 google了一下,说是新版compass已经不包括compass-bluprint ...

  8. NOIP2016DAY1题解

    https://www.luogu.org/problem/lists?name=&orderitem=pid&tag=33%2C83 T1:玩具谜题 题解: 沙茶模拟 #includ ...

  9. (简单) POJ 2240 Arbitrage,SPFA。

    Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit o ...

  10. csv和excel的区别

    excel 文件只能通过excel打开,里面包含公式或者计算. csv文件是一种通用数据格式,可以用很多方式打开,比如excel.csv 以分割数据,用行分割符号分割行级数据,直接上个例子一目了然. ...