Getting Started with WPF self-Learning (vii) WPF initial knowledge binding

Source: Internet
Author: User

Today to record the basis of the binding and the specific use of the binding, in WPF, binding is a very important feature, in the traditional Windows software, most are the UI driver mode, can also say that the event driver, This program pattern is deeply rooted in programmers who have worked for a few years, and as a WinForm upgrade, WPF changes the UI driver completely, and the core goes back to the data-driver pattern, so the program goes back to the algorithm and data. Data, is really need to focus on processing!

Binding plays a vital role in data-driven mode, and one of its most important features is communication, which connects the front and back. First look at the simplest way to use the binding:

One, the binding between elements

Here are 3 controls, Slider,textbox,label, where both the TextBox and Label are the targets, the slider as the data source, the values in the slider to two controls, the slider, the textbox will automatically display value This is the value of fontsize. Since all two bindings have two-way bindings set, you can enter values in the text box, lose focus, and return feedback.

Take a look at the binding statements in XAML, where the elementname is to make the name of the object to bind, path is the dependency property to bind, mode is the binding method, here is the mode there are five ways:

(1) OneWay One-way binding

(2) TwoWay bidirectional binding

(3) OneTime, initially sets the target property based on the Source property value, ignoring all changes later, that is, initializing only.

(4) OneWayToSource, this is the opposite of OneWay.

(5) Default, which is set automatically according to the target property.

If you change the value in the TextBox to something else, the slider position does not change and the font size does not change. When the textbox loses focus, a corresponding change occurs. This is because of the default update mechanism in this binding, the update mechanism Binding.updatesourcetrigger, which has 4 enumeration values

(1) PropertyChange, when the value changes, it is updated.

(2) LostFocus, when the focus of the time to update.

(3) Explicit, when the use of the Bingingexpression.updatesource () method is updated, other conditions are not updated.

(4) Default form

Note: The above four update mechanism settings will only affect the source data, without affecting the target data.

Two: The binding of the element itself

In addition to binding other elements, you can also bind other properties of itself, such as the Opacity property of the slider itself and its own value property bindings, which are gradually hidden when the slider moves to the left.

Third: The binding between the background data and the elements

Before the binding between the elements and the binding of the elements themselves, the final focus is on the background data and the binding of the foreground element, which is a good way to embody the data driver operation mode.

First new Person class

Page background Code

Display effect:

The way data is bound has been written. Binding is a highway, so in order to improve the legitimacy and effectiveness of data transmission, we have to build a series of checkpoints in this highway, some to convert data, and some to verify the data, the following is the binding on the data checksum conversion.

A Data validation of the binding

The data validation work of the binding is a custom class that derives from the ValidationRule class and overrides the Validate Method! Take a look at the example:

First, design a validation class that inherits the ValidationRule class and overrides the Validate method. This class is used to set the checksum when creating the binding.

The code is as follows:

Okay, look at the results.

Because the value passed in value cannot be more than 0~100, the red border is displayed when it is exceeded. In the binding, the default is that the data source is definitely correct, so if the textbox as a data source, and slider as the target, the data source input error is not displayed, then how to solve this problem, Set vdatavalidationrule.validatesontargetupdated = true;

Two Data conversion of binding

The other mechanism of binding is called data conversion, and we can add a data converter (convert) when the value specified by the source side of the path property is inconsistent with the target attribute specified by the target. The problem we mentioned above is actually the conversion of double and stirng types, because it's easier to handle, so the WPF class library does what it does for us, but some data type conversions are not what WPF can do for us, and when we encounter these situations, We can only write converter by ourselves, by creating a class and having the class implement the IValueConverter interface.

This is the date conversion class, which has two methods:

1. When the value is propagated from the binding source to the binding target, the method is called convert

2. Call this method Convertback when the value is propagated from the binding target to the binding source, and the implementation of the method Convertback must be a reverse implementation of the method convert.

These two methods are written in the inside how to convert, to what type is the return type.

Here's how to use:

Well, set the converter property of this binding to an instance of the transformation class we designed. Look at the interface of the program

Getting Started with WPF self-Learning (vii) WPF initial knowledge binding

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.