directory

Controls and layout

2. Binding Basis

preface

“A bridge flying north and South, natural rift into a thoroughfare”

Written in 1956, 1957 Wuhan Yangtze River bridge completed, known as: a bridge flying north and south, big cut into the road. It vividly describes the grand momentum of wuhan Yangtze River Bridge and its important role in the north-south traffic in China.

Haha, why say such a nonsense?

The word Binding gives us the sense of “Binding” at first sight. Wrong, wrong, wrong

Binding is like a “bridge”

The forehead… Yes, it’s a bridge, and the word Bind actually has an associated meaning when used as a verb.

In other words, Binding focuses more on expressing that it is a kind of association like a bridge

The data interaction between the UI layer and the logical layer is completed by Binding. When the attribute of the logical layer changes, the Binding will be notified. When the Binding knows which attribute is changed, it will notify the control associated with the UI end and complete the corresponding action.

How does a property send notifications?

attribute

Shortcuts “propf”

 private int myVar;

 public int MyProperty
 {
     get { return myVar; }
     set{ myVar = value; }}Copy the code

There’s no way for a property to notify, like if I change the value to 3, it’s not going to notify somebody and say, “Look! I became small 3!!” .

It would be “the deuce!” .

INotifyPropertyChanged to make properties speak

We are great programmers, and it is not easy to make attributes speak, we have magic!

INotifyPropertyChanged interface

Notification of property changes

We create a Student class that implements this interface.

“Ability to speak” PropertyChanged

The interface implements an event by default,

How do I use this event? Where is it implemented?

  • We know that it is “to notify when the value of a property changes”.
  • Property is assigned to a set method, so our event is executed in the set body.

When you do, you’re probably more likely to see this

The parameters of the PropertyChanged

Ok!! How does a property bind if it has the ability?

Binding in c #

First, we place a text box and a button on the page. When the button is clicked, the content of the text box changes as the value of the Binding property changes.

2. In the background code of the form, initialize and bind Binding

Here we derive a few knowledge points:

“Ask canal which get so clear, for the source of living water to”

Asked: why is this “half mu square pond” so clear? And answer: because of this source of living water constantly replenished, it makes it so clear. — Zhu Xi’s Feeling from Reading Books

It’s like what makes our program so dynamic is that it has a good source (water) and a good path (water path).

1.

Binding is not so demanding on a data source, as long as it is an object and exposes its own data through attributes (set; The get;) , which can be used as a source for Binding.

As if we were using Student’s as the data source, Student’s public property (Name) lets us get the information.

2, Path

The data is public. How do we get it?

We need the Path to it. The Path to Binding is of type PropertyPath and the argument is a string. That is, the Name of the property.

We use the name of the property as the access path.

3, How to bind controls to Binding

We have instantiated a Binding and set the Source and Path. Now we need to associate it with the text box.

BindingExpressionBase.SetBinding(DependencyObject target, DependencyProperty dp, BindingBase binding)

Parameters:

  • Target: indicates the binding target of the binding.
  • Dp: Target property of the binding.
  • Binding: describe the binding System. Windows. Data. BindingBase object.

Binding: Binding

1, this. Name. SetBinding ()

2, the new Binding () {}

The Binding in the xaml

First, the constructor changes

Binding a Source in xAML code is cumbersome. Also, we usually BindingSource in xAML code only when static resources or control values are used as data sources.

DataContext DataContext

  • What happens when a Binding has no Source? It takes the DataContext of the current object as the data source.
  • The DataContext property is defined in the FrameworkElement class, which is the base class for WPF controls, meaning that all controls have this property.
  • If the current object’s DataContext is empty, it will “borrow” like its parent.

So we could also write it like this

Write XAML code

Iii. Interpretation of Binding in XAML

  • Text = “{Binding Path = Name}”
  • Interpretation: Text is not assigned a Binding value.
  • Bind a data source to the TextBox property Text. Think of this as a Binding constructor that initializes a Binding

Remember the way we wrote it in the beginning

Binding Static resources and controls

1. Associate static resources

Resource

Each control has a Resource property that we can store resources in for easy access.

Here we instantiate a Student and set a key= “Student”.

Source={StaticResource student}

Static resources are accessed through StaticResource, and the key is used to determine which data source to associate.

2. Associated controls

Let’s implement an effect where when you slide the progress bar, the text shows the value of the slide, the text box enters the value, and the progress bar slides to the appropriate position.

(method 1) place two controls and associate the controls in C#

We’ll notice that the progress bar display changes only when the value of the text box is out of focus. Let us set the: UpdateSourceTrigger = UpdateSourceTrigger PropertyChanged (the duty changes trigger)

(Method 2) Place two controls and associate the controls in xAML

The “/” usage of BindingPath

When an attribute of our data source is a collection, we need the child elements of the collection. How to specify Path?

We’re creating a class that has a Name attribute and a Student collection that represents multiple students.

If the data source attribute is a collection, treat the children of the collection as a Path, using the multi-slash syntax, all the way “down”.

At the end

There are too many Binding uses, the rest of the data verification and data conversion, I will do next period, of course I may be lazy summary, but I will try to explain in plain English. Be sure to experiment a lot and you’ll find Binding fun.

“What you can’t have, someone else has for you.”