WPF Binding Value Converter Valueconverter Introduction to use (ii)-imultivalueconverter

Source: Internet
Author: User

Note:

The Imultivalueconverter interface needs to be inherited, and the interface uses the same logic as IValueConverter.

One, Multibinding+converter multi-value binding and multi-value conversion example

The LED should be green when the longitudinal flow is greater than the horizontal flow, and the LED should be red when the longitudinal flow is less than the horizontal flow, otherwise the LED is yellow.

1, custom Colorconverter class, at this time convert in the parameter is object[] values,values[0] corresponding to the first binding value in MultiBinding, here is the vertical flow value, and so on , you can Multiple bindings are specified in the MultiBinding object.

 Public classcolorconverter:imultivalueconverter{//Forward Modification     Public ObjectConvert (Object[] values, Type targetType,Objectparameter, CultureInfo culture) {        if(Values = =NULL|| Values. Length <2)            returnDependencyproperty.unsetvalue; DoubleVervalue = (Double) values[0]; DoubleHorvalue = (Double) values[1]; if(Vervalue >horvalue)return NewSolidColorBrush (Colors.green); Else if(Vervalue <horvalue)return NewSolidColorBrush (colors.red); return NewSolidColorBrush (Colors.yellow); }    //Reverse Modification     Public Object[] Convertback (ObjectValue, type[] targettypes,Objectparameter, CultureInfo culture) {        //return NULL, token non-bidirectional conversion        return NULL; }}

2.Xaml definition

Add a Namespace

Xmlns:local= "Clr-namespace:audiodemo.view"
<window.resources>    <Local:colorconverterx:key= "Cvtcolor"/></window.resources><Grid>    <Labelx:name= "Label"Content= "Portrait Value:"HorizontalAlignment= "Left"Margin= "10,40,0,0"VerticalAlignment= "Top"/>    <Labelx:name= "Label1"Content= "Transverse value:"HorizontalAlignment= "Left"Margin= "10,80,0,0"VerticalAlignment= "Top"/>    <Sliderx:name= "Sliderver"HorizontalAlignment= "Left"Margin= "75,43,0,0"VerticalAlignment= "Top"Width= "192"/>    <Sliderx:name= "Sliderhor"HorizontalAlignment= "Left"Margin= "75,81,0,0"VerticalAlignment= "Top"Width= "192"/>    <EllipseHorizontalAlignment= "Left"Height= "+"Margin= "75,120,0,0"Stroke= "Black"VerticalAlignment= "Top"Width= "+">        <Ellipse.fill>            <multibindingConverter="{StaticResource Cvtcolor}">                <BindingPath= "Value"elementname= "Sliderver"/>                <BindingPath= "Value"elementname= "Sliderhor"/>            </multibinding>        </Ellipse.fill>    </Ellipse></Grid>

Two, RGB color mixed instance

1. Converter definition

 Public classrgbconverter:imultivalueconverter{//forward modification, integrating color values     Public ObjectConvert (Object[] values, Type targetType,Objectparameter, CultureInfo culture) {        if(Values = =NULL|| Values. Length <3)            return NULL; byteR = System.Convert.ToByte (values[0]); byteg = System.Convert.ToByte (values[1]); byteb = System.Convert.ToByte (values[2]); Color Col=Color.fromrgb (R, G, b); SolidColorBrush Brush=NewSolidColorBrush (COL); returnBrush; }     Public Object[] Convertback (ObjectValue, type[] targettypes,Objectparameter, CultureInfo culture) {        return NULL; }}

2.Xaml definition

Don't forget to add namespaces first

Xmlns:local= "Clr-namespace:audiodemo.view"
<window.resources>    <Local:rgbconverterx:key= "RGBCVT"/></window.resources><Grid>    <Labelx:name= "Label"Content= "Red:"HorizontalAlignment= "Left"Margin= "10,48,0,0"VerticalAlignment= "Top"/>    <Labelx:name= "Label_copy"Content= "Green:"HorizontalAlignment= "Left"Margin= "7,85,0,0"VerticalAlignment= "Top"/>    <Labelx:name= "Label_copy1"Content= "Blue:"HorizontalAlignment= "Left"Margin= "7,123,0,0"VerticalAlignment= "Top"/>    <Sliderx:name= "Slider_r"Minimum= "0"Maximum= "255"Ticks= "1"HorizontalAlignment= "Left"Margin= "68,53,0,0"VerticalAlignment= "Top"Width= "207"/>    <Sliderx:name= "Slider_g"Minimum= "0"Maximum= "255"Ticks= "1"HorizontalAlignment= "Left"Margin= "68,91,0,0"VerticalAlignment= "Top"Width= "207"/>    <Sliderx:name= "Slider_b"Minimum= "0"Maximum= "255"Ticks= "1"HorizontalAlignment= "Left"Margin= "68,124,0,0"VerticalAlignment= "Top"Width= "207"/>    <RectangleHorizontalAlignment= "Left"Height= "All"Margin= "68,160,0,0"Stroke= "Black"VerticalAlignment= "Top"Width= "142">        <Rectangle.fill>            <multibindingConverter="{StaticResource RGBCVT}">                <Bindingelementname= "Slider_r"Path= "Value"></Binding>                <Bindingelementname= "Slider_g"Path= "Value"></Binding>                <Bindingelementname= "Slider_b"Path= "Value"></Binding>            </multibinding>        </Rectangle.fill>    </Rectangle></Grid>

Operation Result:

More information: WPF binding value Converter valueconverter usage profile (i)

WPF Binding Value Converter Valueconverter Introduction to use (ii)-imultivalueconverter

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.