Silverlight converter classic demo

Source: Internet
Author: User

If you enter a value greater than 0 in the text box, an up arrow is displayed. If you enter a value equal to 0, a horizontal line is displayed. If you enter a value less than 0, a downward arrow is displayed.

 

 

Foreground XAMLCode:

 <  Usercontrol  Xmlns  = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"  Xmlns: x  = "Http://schemas.microsoft.com/winfx/2006/xaml"  Xmlns: d  = "Http://schemas.microsoft.com/expression/blend/2008"  Xmlns: MC = "Http://schemas.openxmlformats.org/markup-compatibility/2006"  MC: ignorable  = "D"  Xmlns: Local  = "CLR-namespace: class4samples"  X: Class  = "Class4samples. bindingconverter"  D: designwidth  = "640"  D: designheight  = "480"  >      <  Usercontrol. Resources >          <  Local: numbertoarrowstyleconverter  X: Key  = "Numbertoarrowstyleconverter"  />      </  Usercontrol. Resources  >      <  Grid  X: Name  = "Layoutroot"  >          <  Stackpanel Orientation  = "Horizontal"  D: layoutoverrides  = "Width"  Verticalalignment  = "Center"  Horizontalalignment  = "Left"  Margin  = "200,0, 0, 0"  >              <  Textbox  X: Name  = "Textbox"  Text = "-1"  Textwrapping  = "Wrap"  Verticalalignment  = "Center"  />              <  Textblock  Text  = "%"  Textwrapping  = "Wrap"  Verticalalignment  = "Center"  />              < Contentcontrol  Content  = "Contentcontrol"  Style  ="  {Binding text, elementname = Textbox, mode = oneway, converter = {staticresource numbertoarrowstyleconverter }}  "  />          </  Stackpanel  >      </  Grid  >  </ Usercontrol  > 

 

Backend CS code:

View code

 Using  System;  Using  System. windows;  Using  System. Windows. controls;  Using  System. Windows. documents;  Using  System. Windows. Ink;  Using System. Windows. input;  Using  System. Windows. Media;  Using  System. Windows. Media. animation;  Using  System. Windows. shapes;  Namespace  Class4samples {  Public   Partial   Class  Bindingconverter: usercontrol {  Public Bindingconverter (){  //  Required to initialize Variables  Initializecomponent ();}}} 

 

As you can see, the last line of code is not written.

We use a converter.

 

The Code is as follows:

 Using  System;  Using  System. Collections. Generic;  Using  System. text;  Using  System. windows; Using  System. Windows. controls;  Using  System. Windows. Data;  Using  System. Windows. documents;  Using  System. Windows. input;  Using  System. Windows. Media;  Using  System. Windows. Media. imaging;  Using  System. Windows. shapes;  Namespace Class4samples {  Public   Class  Numbertoarrowstyleconverter: ivalueconverter {  # Region Ivalueconverter members Public   Object Convert ( Object Value, type targettype, Object  Parameter, system. Globalization. cultureinfo culture ){  Double  Number;  If (Value! = Null && Double . Tryparse (value. tostring (), Out  Number )){  If (Number> 0.0  ){  Return App. Current. Resources [ "  Upstyle  "  ];}  Else  If (Number < 0.0  ){  Return App. Current. Resources [ "  Downstyle  "  ];}  Else  {  Return App. Current. Resources [ "  Normalstyle  " ] ;}}  Else  {  Return   Null  ;}}  Public   Object Convertback ( Object Value, type targettype, Object  Parameter, system. Globalization. cultureinfo culture ){  Throw   New Notimplementedexception ();}  # Endregion  }} 

 

In this way, the above requirements can be fulfilled.

 

Another trick: after writing a converter, you do not need to manually write The XAML code in the XAML to reference it.

In the blend, in the data panel,

Click "create sample data from Class", find the desired Converter Based on the converter name, name it, and click "OK. You can import it to The XAML.

 

When binding, you can use tools to bind the blend.

 

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.