首页 技术 正文
技术 2022年11月6日
0 收藏 604 点赞 446 浏览 2300 个字

WPF MultiBinding 和 IMultiValueConverter

时间 2015-02-02 19:43:00  博客园精华区原文  http://www.cnblogs.com/woodenmancool/p/4268539.html主题 WPF

MultiBinding,描述附加到单个绑定目标属性的Binding对象的集合。可以指定多个数值绑定。

IMultiValueConverter通过转换器使用MultiBingding对象,该对象讲根据这些绑定的值转换生成绑定目标的最终值(效果)。

可以看一下微软给出的案例:

 1 public class NameConverter : IMultiValueConverter
2 {
3 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
4 {
5 string name;
6
7 switch ((string)parameter)
8 {
9 case "FormatLastFirst":
10 name = values[1] + ", " + values[0];
11 break;
12 case "FormatNormal":
13 default:
14 name = values[0] + " " + values[1];
15 break;
16 }
17
18 return name;
19 }
20
21 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
22 {
23 string[] splitValues = ((string)value).Split(' ');
24 return splitValues;
25 }
26 }

在资源中定义引用转换

1 <c:NameConverter x:Key="myNameConverter"/>
1 <TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
2 <TextBlock.Text>
3 <MultiBinding Converter="{StaticResource myNameConverter}"
4 ConverterParameter="FormatLastFirst">
5 <Binding Path="FirstName"/>
6 <Binding Path="LastName"/>
7 </MultiBinding>
8 </TextBlock.Text>
9 </TextBlock>

Orlando Bloom Bloom, Orlando

亦或者 如果股票买卖数据中需要定义 红涨绿跌 ,则会对比昨收价对比实时价格

 1 public class QDataColorConvert : IMultiValueConverter
2 {
3 /// 需传入一组对象,(基础值 比对值)
4 public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
5 {
6 double proNum = Math.Round((double)values[1], 2);//目前实时阶段性价格
7 double basepronum = Math.Round((double)values[0], 2);//昨收价格
8
9 if (proNum > basepronum)
10 {
11 return new SolidColorBrush(Color.FromArgb(255, 255, 96, 96));
12 }
13 else if (proNum < basepronum)
14 {
15 return new SolidColorBrush(Color.FromArgb(255, 83, 187, 108));
16 }
17 return new SolidColorBrush(Color.FromArgb(255, 227, 227, 227));
18 }
19
20 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
21 {
22 throw new NotImplementedException();
23 }
24 }

如何使用呢?

 1 <C:QDataColorConvert x:Key="Qdataconverter"/>
2
3 <TextBlock Text="{Binding Path=Newprice}">
4 <TextBlock.Foreground>
5 <MultiBinding Converter="{StaticResource Qdataconverter}">
6 <Binding Path="Baseprice"/>
7 <Binding Path="Newprice"/>
8 </MultiBinding>
9 </TextBlock.Foreground>
10</TextBlock>

【转】WPF MultiBinding 和 IMultiValueConverter

Baseprice;Newprice就是数据模型中的实时数据(依赖属性),这样就可以做对比。当然 这里的Binding 与IValueConverter这里就只用到当个数据绑定,单个对应值转换。

这个就是介绍的WPF的MultiBinding 和 IMultiValueConverter的简短文字

希望和大家多多的交流沟通,共同进步。 谢谢!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898