博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone MultiBinding :Cimbalino Toolkit
阅读量:7297 次
发布时间:2019-06-30

本文共 4322 字,大约阅读时间需要 14 分钟。

本文转自 sun8134 博客园博客,原文链接: http://www.cnblogs.com/sun8134/p/3186066.html  ,如需转载请自行联系原作者

 

这个有啥用呢,引用下MSDN的例子:

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 andlastName. The following example produces a  that shows the first and last names of a person with the last name first.

 
ConverterParameter="FormatLastFirst">
 
 
 
 
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 需要自己来实现(方法见高手博客:)

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

 

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

 

我们先新建一个wp工程(7.1,8.0都行,Cimbalino toolkit is compatible with the  and ,这里我用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

 

 

添加页面资源

 

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

 
 
 
 
 
 
 

 

最后数据绑定:

ObservableCollection
StudentScore = new ObservableCollection
();
 
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

 

另外还有许多其他的功能

大家可以试试

 

 

源码:

 

 

 

参考:

你可能感兴趣的文章
LightOJ - 1261 K-SAT Problem
查看>>
小程序的曲折
查看>>
virtualbox+centos 7 实现宿主机器互通
查看>>
好记性不如烂笔杆-android学习笔记<四> 布局用控件简单介绍
查看>>
“Device eth0 has different MAC address than expected, ignoring.”问题
查看>>
HDU 4871 Shortest-path tree
查看>>
webapp开发学习---Cordova环境搭建
查看>>
Spring 框架学习整理
查看>>
Eric5 for Python 3.3.3安装指南
查看>>
gulp打包
查看>>
python模块之datetime
查看>>
C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库...
查看>>
【转载】推荐给初创业者:一个小公司老板的日常管理总结
查看>>
通过安全浏览保护 WebView
查看>>
C# Regex.IsMatch()正则表达式验证
查看>>
53.使用$.extend()扩展工具函数
查看>>
Jquery 表格操作,记录分页情况下,每一页中被用户勾选的信息
查看>>
使用ASP.NET MVC+Entity Framework快速搭建系统
查看>>
fg、bg、jobs、&、nohup、ctrl+z、ctrl+c 命令
查看>>
stm32手册上的英文
查看>>