在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/

最新文章

  1. 窥探Vue.js 2.0
  2. bzoj 2245: [SDOI2011]工作安排
  3. 转 Eclipse下svn的创建分支/合并/切换使用
  4. 文件操作2 cp mv rm
  5. call()与apply()传参需要注意的一点
  6. hihoCoder 1252 Kejin Game
  7. 36.java_exception_test
  8. elasticsearch java和_head插件对索引文档的增删改查
  9. python语言中的AOP利器:装饰器
  10. python—列表生成式
  11. Mysql+Keepalived双主热备高可用操作记录
  12. java.lang.NumberFormatException 错误及解决办法
  13. ruby离线安装整理
  14. Difference between MB Star C3 and MB Star C4
  15. JDK(java se development kit)的构成
  16. apache中的directory 和virtualhost有啥区别和联系呀
  17. [hadoop读书笔记] 第十五章 sqoop1.4.6小实验 - 将mysq数据导入HBASE
  18. 【安全开发】Perl安全编码规范
  19. poj1661 (DP)
  20. Firefox浏览器【书签工具栏】里的网址链接无法删除的解决办法

热门文章

  1. ansible 常见指令表
  2. 待解决输入istream_iterator
  3. 正则表达式处理BT的html嵌套问题
  4. Django 中的 model 继承
  5. ffmpeg h264 encdoer 速度对比
  6. Mac 系统重新安装的几种方法
  7. php markdown 接口文档生成工具 SummerDoc
  8. 14)django-模板(计数器)
  9. GIT 版本管理-github&amp;码云
  10. 使用WebBrowser控件播放Flash网页相关问题解决方法(转)