在WPF和WIN8中是支持MultiBinding

这个有啥用呢,引用下MSDN的例子http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

MultiBinding allows you to bind a binding target property to a list of source properties and then apply logic to produce a value with the given inputs. This example demonstrates how to use MultiBinding.

In the following example, NameListData refers to a collection of PersonName objects, which are objects that contain two properties, firstName and lastName. The following example produces a TextBlock that shows the first and last names of a person with the last name first.

<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myNameConverter}"
                  ConverterParameter="FormatLastFirst">
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

 
public class NameConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string name;
 
        switch ((string)parameter)
        {
            case "FormatLastFirst":
                name = values[1] + ", " + values[0];
                break;
            case "FormatNormal":
            default:
                name = values[0] + " " + values[1];
                break;
        }
 
        return name;
    }
 
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        string[] splitValues = ((string)value).Split(' ');
        return splitValues;
    }
}

可惜在wp中这个被砍掉了

想要 MultiBinding 需要自己来实现(方法见高手博客:http://www.devdiv.com/wp_amp_win_-blog-55433-51080.html

当然也有一个稍微简单点的方法,使用Cimbalino Windows Phone Toolkit(http://cimbalino.org/),Cimbalino Toolkit 的作者为我们提供了一个MultiBindingBehavior,可以比较方便的实现 MultiBinding

下面看一个实例把,这里我们模拟一个显示学生各科成绩的应用,在应用中有一个ListPicker,应用会根据ListPicker的值来作为阈值,对成绩的颜色进行修改,下面是实现:

我们先新建一个wp工程(7.1,8.0都行,Cimbalino toolkit is compatible with the Windows Phone SDK 7.1.x and Windows Phone 8.0,这里我用7.1的,主要是机器不行,8.0的模拟器太卡…)

添加成绩类:

namespace PhoneIValueConverterMultibinding1
{
    public class Score
    {
        public string name { get; set; }
        public int number { get; set; }
    }
}

在工程中添加Cimbalino Windows Phone Toolkit

然后定义我们的颜色转换类(由于我没用双向绑定,所以没写ConvertBack…):

using System;
using System.Windows.Media;
using System.Globalization;
using Cimbalino.Phone.Toolkit.Converters;
namespace PhoneIValueConverterMultibinding1
{
    public class ColorChange : MultiValueConverterBase 
    {
        public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length < 2)
            {
                return new SolidColorBrush(Colors.White);
            }
 
            if ((values[0] != null) && (values[1] != null))
            {
                //根据输入,判断返回值
                try
                {
                    int limitnumber = System.Convert.ToInt32((string)values[0]);
                    int studentscore = (int)values[1];
 
                    if (limitnumber < studentscore)
                    {
                        return new SolidColorBrush(Colors.Red);
                    }
                    else
                    {
                        return new SolidColorBrush(Colors.Green);
                    }
                }
                catch (FormatException)
                {
                    return new SolidColorBrush(Colors.White);
                }
                catch (Exception)
                {
                    return new SolidColorBrush(Colors.White);
                }
            }
            else
            {
                return new SolidColorBrush(Colors.White);
            }       
        }
 
        public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

然后在xaml里添加引用:

    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:Local="clr-namespace:PhoneIValueConverterMultibinding1"
    xmlns:cimbalinoBehaviors="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"

添加ListPicker

                <toolkit:ListPicker Header="成绩阈值" Name="listpicker1" SelectionChanged="listpicker1_SelectionChanged">
                    <toolkit:ListPickerItem Content="50" />
                    <toolkit:ListPickerItem Content="60" />
                    <toolkit:ListPickerItem Content="70" />
                    <toolkit:ListPickerItem Content="80" />
                    <toolkit:ListPickerItem Content="90" />
                </toolkit:ListPicker>

添加页面资源

    <phone:PhoneApplicationPage.Resources>
<Local:ColorChange x:Key="ColorConverter" />
</phone:PhoneApplicationPage.Resources>

然后添加绑定的listbox,并在其中添加MultiBindingBehavior ,因为我们要改变的是文字的颜色,所以设置PropertyName="Foreground"

                <ListBox Height="510" Name="lb1" Margin="12,12,0,0" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Width="432" Orientation="Horizontal">
                                <TextBlock TextWrapping="Wrap" Text="{Binding name}" FontSize="32" Width="200" />
                                <TextBlock TextWrapping="Wrap" Text="{Binding number}" FontSize="32" Margin="0" >
                                        <i:Interaction.Behaviors>
                                            <cimbalinoBehaviors:MultiBindingBehavior Converter="{StaticResource ColorConverter}" PropertyName="Foreground"  >                                                
                                                <cimbalinoBehaviors:MultiBindingItem Value="{Binding ElementName=listpicker1, Path=SelectedItem.Content}" />             
                                                <cimbalinoBehaviors:MultiBindingItem Value="{Binding number}" />
                                            </cimbalinoBehaviors:MultiBindingBehavior>
                                        </i:Interaction.Behaviors>
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>                    
                </ListBox>

最后数据绑定:

        ObservableCollection<Score> StudentScore = new ObservableCollection<Score>();
 
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            StudentScore.Add(new Score { name = "语文", number = 55 });
            StudentScore.Add(new Score { name = "数学", number = 66 });
            StudentScore.Add(new Score { name = "英语", number = 76 });
            StudentScore.Add(new Score { name = "物理", number = 72 });
            StudentScore.Add(new Score { name = "化学", number = 97 });
            StudentScore.Add(new Score { name = "生物", number = 86 });
            StudentScore.Add(new Score { name = "地理", number = 68 });
            StudentScore.Add(new Score { name = "历史", number = 95 });
            StudentScore.Add(new Score { name = "政治", number = 39 });
  }
 
        private void listpicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listpicker1 != null)
            {
                if (listpicker1.SelectedIndex != -1)
                {
                    lb1.ItemsSource = StudentScore;
                }
            }            
        }

看看效果:

最后说下Ms强烈建议不要在复杂绑定中是使用Converter

另外Cimbalino Windows Phone Toolkit还有许多其他的功能

大家可以试试

源码:

参考:

http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

https://github.com/Cimbalino/Cimbalino-Phone-Toolkit

http://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/

Windows Phone MultiBinding :Cimbalino Toolkit的更多相关文章

  1. 2016 windows安装phing:安装成功

    21:39 2016/7/212016 windows安装phing:安装成功注意:出现错误时就去更新pear:参见:http://www.cnblogs.com/pinnasky/archive/2 ...

  2. Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes

    Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes http://blog.csdn.n ...

  3. Windows服务一:新建Windows服务、安装、卸载服务

    Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面 ...

  4. zabbix windows angent安装:

    zabbix windows angent安装:1.下载zabbix agent for windows客户端,直接解压到C盘下.C:\zabbix 的目录015/04/21 11:16 <DI ...

  5. windows phone8.1:Xml,Json序列化和反序列化

    原文:windows phone8.1:Xml,Json序列化和反序列化 小梦本例主要实现以下四点内容: 将Car对象序列化为xml 将Car对象序列化为Json 将xml反序列化为Car对象 将js ...

  6. 老司机实战Windows Server Docker:2 docker化现有iis应用的正确姿势

    前言 上一篇老司机实战Windows Server Docker:1 初体验之各种填坑介绍了安装docker服务过程中的一些小坑.这一篇,我们来填一些稍大一些的坑:如何docker化一个现有的iis应 ...

  7. 老司机实战Windows Server Docker:3 单节点Windows Docker服务器简单运维(上)

    经过上两篇实战Windows Server Docker系列文章,大家对安装Windows Docker服务以及如何打包现有IIS应用为docker镜像已经有了基本认识.接下来我们来简单讲讲一些最基本 ...

  8. 老司机实战Windows Server Docker:5 Windows Server Dockerfile葵花宝典

    前面两篇(简单运维1.简单运维2)介绍了一些Windows Server Docker相关的基本运维知识.今天这一篇,Windows Server Dockerfile葵花宝典,涵盖了许多典型场景的W ...

  9. 自动化测试框架【windows版】:JMeter + Ant + Jenkins

    前提条件:windows安装了jmeter.ant.jenkins 安装方法参考汇总目录中对应的博文 截图看不清的,可以调大浏览器倍数看 jenkins驱动ant执行,ant驱动jmeter执行 an ...

随机推荐

  1. 当linux中的所有指令突然不能使用的时候

    接到同事电话,线上linux系统所有命令执行不了(由于其误操作执行一些命令) 此时可以按以下步骤解决问题: 1.首先导入临时变量(重启虚拟机之后失效),使得所有命令行暂时可以用 直接在命令行执行以下命 ...

  2. 题解-洛谷4921&4931 情侣?给我烧了!(加不加强无所谓版)

    Problem 简单版 & 加强版 题目概要(其实题面写得很清楚,这里搬运一下): \(n\) 对情侣排座位,恰有 \(n\) 排座位,每排 \(2\) 个座位,在一个就座方案中所有人会将将座 ...

  3. <TCP/IP>地址解析协议ARP

    从前两章中有学到,网络层地址和链路层地址是由不同的,一个是物理地址,一个是IP地址.物理地址固定存储在网卡中,不会改变,而IP地址是可以网络管理员和用户自己分配的 在传统的IPv4网络中,一台A主机要 ...

  4. Spring+Hibernate 多数据源不同事务创建

    环境:Spring 3.0 ,Hibernate 3.5 ,同类型数据库(DB2) 编前语:此片仅粗略的描述使用Spring和Hibernate采用注入方式管理多数据源在不同事务的情况下使用的方法. ...

  5. 【转】RESTful Webservice创建

    RESTful Web Services with Java   REST stands for REpresentational State Transfer, was first introduc ...

  6. vuex之 mapState, mapGetters, mapActions, mapMutations 的使用

    一.介绍 vuex里面的四大金刚:State, Mutations,Actions,Getters (上次记得关于vuex笔记 http://www.cnblogs.com/adouwt/p/8283 ...

  7. Go Rand小结

    对于Random的使用,在业务中使用频率是非常高的,本文就小结下常用的方法: 在Golang中,有两个包提供了rand,分别为 "math/rand" 和 "crypto ...

  8. HDU 5514

    题意: 给你 N 个数 和 一个 M: 对于 每一个 Ni , 乘以 K 取摸 M 都有一个 集合, 把所有集合合并, 求和 Σ ai ( ai → K * Ni % M ) 思路 : 最开始 直接求 ...

  9. Swap交换分区概念

    什么是Linux swap space呢?我们先来看看下面两段关于Linux swap space的英文介绍资料: Linux divides its physical RAM (random acc ...

  10. HTML 页面meta标签

    1. 概述 1.1 说明 <meta>标签提供了HTML文档的元数据[元数据(Metadata)是数据的数据信息],即页面的元信息,元数据不会显示在客户端,但是会被浏览器解析.meta元素 ...