WPF MultiBinding and IMultiValueConverter, wpfmultibinding

Source: Internet
Author: User

WPF MultiBinding and IMultiValueConverter, wpfmultibinding

MultiBinding: a set of Binding objects appended to a single Binding target attribute. You can bind multiple values.

IMultiValueConverter uses the MultiBingding object through the converter. This object generates the final value (effect) of the binding target based on the conversion of these bound values ).

Let's take a look at the case given by Microsoft:

 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 }

Define reference conversion in resources

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, Orlando

 

Or, if the stock sales data needs to be definedRed, red, and green, The real-time price will be compared with the renewal price

1 public class QDataColorConvert: IMultiValueConverter 2 {3 // you need to input a group of objects (Basic Value Comparison value) 4 public object Convert (object [] values, Type targetType, object parameter, system. globalization. cultureInfo culture) 5 {6 double proNum = Math. round (double) values [1], 2); // current real-time phase price 7 double basepronum = Math. round (double) values [0], 2); // returns the price of 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}

How to use it?

 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 is the real-time data (dependency attribute) in the data model.

Of course, the Binding and IValueConverter here are only used to bind a single data and convert a single corresponding value.

This is the brief text of the MultiBinding and IMultiValueConverter of WPF.

I hope to communicate with you a lot and make progress together. Thank you!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.