SILVERLIGHT3 Series (vii) Data Binding 3 data Converte

Source: Internet
Author: User
Tags silverlight

SILVERLIGHT3 Series (vii) Data Binding 3 Data Converter

7 Data Conversion

In normal circumstances, the data from the background to the foreground display, there is no change. It seems logical, but it may not be the effect you want, the data source data may be low-level (where the low level is that the data is primitive, or that the database is understandable, not the end user can understand), you do not want him to directly display in the interface. For example, you might turn a number into a form that users can understand, or a string that wants the date to show up in a format. If so, you need to convert the data into the correct display form. If it is a two-way binding, you also need to convert the user-supplied data into a form that the database can store.

Fortunately, Silverlight allows you to create a value conversion class to complete. This value conversion class is responsible for converting the value of the database to a value that can be displayed, and, if it is two-way binding, to convert the value entered by the user to a value that the database can store.

In data binding, value conversions are very common. You can use them in the following situations:

1 format the data into a string. For example, converting a number to a string is the most common way, but not the only feature.

2 Create a special Silverlight type. For example, you create some binary data that you read into a BitmapImage object so that you can bind it to an image control.

3 conditionally change some properties of a bound data. For example, change the background color of a control by a value conversion class, or highlight a part of it.

7.1 Changing strings using value conversion format

Value conversions are a good tool when you need to display a string from a number. For example: The unit price attribute of one of your products, you may use decimal storage in the database, but you need to display 3.9900 when displayed, or you need to display a money symbol, just like ¥49.99.

You can create a value conversion class from the following steps.

1 Create a class that implements the IValueConverter interface (below the System.Windows.Data space), placing this class in your Silverlight project, not in the WebService project.

2 Implement the Convert () method to convert the original value to a value that can be displayed.

3 implements the Convertback () method, which, in turn, converts the displayed value to the original value.

The diagram above is a schematic diagram of a transformation.

In the Decima value L to the currency currency conversion, you can use the Decimal.tostring () method to complete, you need to set the transformation result form "C"

String currentytext=decimalprice.tostring ("C");

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.