Data binding (2) (translation)

zhaozj2021-02-16  57

Change attributes:

Now we know the data binding, let's take a look at how objects support automatic attribute change. When we change the data of the object in the program, it is difficult to let us know the change in the control. Ui and objects can get the correct data we can't see. .

What we need is an object to remind the UI, attribute value changes at any time. And these we can use the event's statement

Reflective, when data binding, data binding is based on the attribute change of the event, which is the object's attribute name.

Example: We just defined a ID attribute, when the data binds to the control, the binding begins to listen

The ID attribute changes this event. ID changes the event, the corresponding object changes.

We can explain our Order class by declaring these events:

Public Class Order

Public Event IDChange AS EventHandler

Public Event CustomerChanged As EventHandler

These events are declared through EventHandler. This requires data binding to know these events, if we do not declare

In these events, when the data binding is generated. EventHandle in the Windows window.

It is a standard event model. These events are defined by Sender and Event. Using event declarations, we ensure

When the attribute changes, the event also generates. This is the best time, and we use the ID attribute to implement:

Public property ID () AS STRING

Get

Return MID

END GET

Set (byval value as string)

MID = Value

RaiseEvent Idchanged (Me, New Eventargs ())

End set

End Property

We create a trigger to capture changes to any place at any time at any place, and a lot of classes are paid by the SET method. This requires us to make a timely event to change these values. For a better For example, the Order class has a lot of LineItem connection objects, let's take a look at the variable declaration of the LineItem class:

Public Class LineItem

Public Event ProductChanged As EventHandler

Public Event QUANTITYCHANGED AS EventHandler

Public Event PriceChanged As EventHandler

Public Event AmountChanged As EventHandler

Private MProduct As String

Private MQUANTITY AS INTEGER

Private MPRICE AS DOUBLE

There are four events, but only three variables, the amount property is calculated by quantity and price.

Public Readonly Property Amount () AS DOUBLE

Get

Return Mquantity * MPRICE

END GET

End Property

This is a read-only attribute, but we can change. In fact, other prices have changed, so we need events to capture it, such as price changes:

Public property price () As Double

Get

Return MPRICE

END GET

Set (ByVal Value As Double)

MPRICE = Value

RaiseEvent PriceChanged (ME, New EventArgs ())

RaiseEvent AmountChanged (ME, New Eventargs ())

End set

End Property

Due to the change of the price, we don't just do the PriceChanged event, but also trigger the event, so that the Amount property changes correctly. These code is an event we have to discover data changes in time.

That is to say, our AmountChanged event is not strict. When the data is bound to the object properties, the data binding is to listen to the PropertyChanged event. Once these time triggered, the object's control will be updated.

In other words, if the form control is bound to the Price and Amount properties, the formChanged event triggered by the form is not only to update the Price property, the same Amount property is also updated.

If we only bind Amount, UI is not working properly because there is no AmountChanged event, which is not working properly. We need to declare the PropertyChanged event for each attribute.

转载请注明原文地址:https://www.9cbs.com/read-21953.html

New Post(0)