专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WPF

WPF MultiBinding 跟 IMultiValueConverter

发布时间:2011-06-23 13:55:57 文章来源:www.iduyao.cn 采编人员:星星草
WPF MultiBinding 和 IMultiValueConverter

  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>

 

 

 

Baseprice;Newprice就是数据模型中的实时数据(依赖属性),这样就可以做对比。

当然 这里的Binding 与IValueConverter这里就只用到当个数据绑定,单个对应值转换。

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

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

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: