WPF (7) Introduction to Binding and wpfbinding

Source: Internet
Author: User

WPF (7) Introduction to Binding and wpfbinding

Today, I will record the basics and specific usage of Binding. Speaking of this Binding, Binding is an important feature in WPF. In the traditional Windows software, most of them are in the UI driver mode. It can also be said that the event driver is rooted in programmers who have been working for several years. WPF is used as a Winform upgrade, it completely changed the UI driver, and the core went back to the data driver mode. In this way, the program went back to algorithms and data. Data is what really needs to be processed!

Binding plays a vital role in the data driver mode. One of its most important features is communication, which connects the foreground and the background. First, let's take a look at the simplest method of using Binding:

I. Binding between elements

 

There are three controls: Slider, TextBox, and Label. Both TextBox and Label are used as targets and Slider are used as data sources. The values in the Slider are presented to the two controls and the Slider is moved, textBox automatically displays the Value, that is, the FontSize Value. Because both bindings are set with Bidirectional binding, you can enter a value in the text box, lose the focus, and report back.

Let's take a look at the binding statement in XAML. The ElementName used here is the name of the object to be bound, Path is the dependency attribute to be bound, and mode is the binding method, there are five modes for mode:

(1) OneWay one-way binding

(2) TwoWay bidirectional binding

(3) OneTime: initially set the target attribute based on the source attribute value, and then ignore all changes, that is, only Initialization is performed.

(4) OneWayToSource, which is opposite to OneWay

(5) Default, which is the Default format and is automatically set based on the target attribute.

If you change the value in TextBox to another one, the slide position is not changed, and the font size is not changed. What is the problem? When the TextBox loses its focus, it changes accordingly. This is because of the default update mechanism in this Binding. The update mechanism Binding. UpdateSourceTrigger has four enumerated values.

(1) PropertyChange: update when the value changes.

(2) LostFocus, which was updated when the focus was removed.

(3) Explicit: When the BingingExpression. UpdateSource () method is called, it will not be updated in other cases.

(4) Default, Default format

Note: The above four update mechanisms only affect the source data, but not the target data.

 

Ii. Binding of Elements

In addition to binding other elements, you can also bind other attributes of the Slider. For example, the Opacity attribute of the Slider is bound to its own Value attribute. When the Slider moves to the left, it is gradually hidden.

Iii. Binding between background data and elements

As mentioned above, the binding between elements and the binding of the elements themselves focuses on the binding of background data and foreground elements. This binding mode reflects the running mode of the data driver.

First, create a Person class.

 

Page background code

 

Display Effect:

 

The data binding method has been completed. Binding is a highway. In order to improve the validity and validity of data transmission, we need to establish a series of checkpoints on this highway. Some are used to convert data, and some are used to verify data, the following describes the data validation and conversion of Binding.

(1) Data Validation of Binding

The data validation of Binding is a custom class derived from the ValidationRule class and overwritten by the Validate method! Take a look at the instance:

 

First, design a validation class that inherits the ValidationRule class and overwrites the Validate method. When this class is used, it is set for verification when Binding is created.

The Code is as follows:

 

Let's take a look at the results.

 

Because the value passed in cannot exceed 0 ~ Between 100, so when it exceeds, the red border is displayed. In Binding, the data source is regarded as correct by default. Therefore, if TextBox is used as the data source and Slider is used as the target, the data source input error is not displayed, so how can we solve this problem? Set VdataValidationRule. validatesOnTargetUpdated = true;

 

(2) Binding data conversion

Binding also has another mechanism called data conversion. When the Path attribute value specified by the Source end is inconsistent with the Target attribute specified by the Target end, we can add a data converter (Convert ). The problem we mentioned above is actually the conversion of the double and stirng types, because the processing is relatively simple, so the WPF class library will help us, however, some data type conversions are not something that WPF can do for us. In these cases, we can only write Converter by ourselves by creating a class and implementing 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 Convert method is called.

2. When the value is transmitted from the binding target to the binding source, this method ConvertBack is called. The implementation of the ConvertBack method must be the reverse implementation of the Convert method.

The two methods respectively write in how to convert, and convert to what type is the return type.

The following is the usage:

 

All right, set the Converter attribute bound to the converted class instance we designed. Let's take a look at the program interface.

 

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.