Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Excuse me: In c#. Net wpf, the use of data.
Excuse me: In c#. Net wpf, the use of data.
The essence of the program is the data addition algorithm, which has been completely implemented in the console application. The user gives an input, and after the algorithm processing, the program will feed back an output. In this process, data is in a dominant and core position. However, due to the increasing popularity of GUI programs, UI-based "message-driven" or "event-driven" makes data in a passive position. In a GUI program, data will always be processed after the event is triggered, and at the same time, it will be displayed on the UI control after the algorithm is executed. How to change the state of data from passive to active in GUI programming and return the data to the core of the program? WPF introduced data binding mechanism for us.

The application will have a three-layer structure, namely, data storage layer, data processing layer and data presentation layer. The binding mechanism works between the processing layer and the presentation layer. Binding is like a bridge. Its two ends are the source and target of data, where the data comes from, the source, the bridge in the middle and the destination of the data. Generally speaking, the source of binding is the object of logical layer, and the target is the control object of UI layer. In this way, data will be continuously transmitted to the UI layer through binding and displayed by the UI layer, thus completing the process of data-driven UI.

After having a general understanding of the concept of binding, let's look at an example to understand it more vividly.

1. First, create a class named Student and use this class as a data source. Student objects have many attributes, so data is what you want to send to UI elements through binding, that is, the elements on UI are most concerned about the change of attribute values, and this attribute is the binding path. However, attributes alone are not enough, because binding is an automatic mechanism. When the value changes, the property should be able to tell Binding and let Binding pass the change to the UI element, which will trigger the PropertyChanged event in the Set statement of the property. When the data source is set to binding, the binding will automatically listen for this event.

Class students: INotifyPropertyChanged

{

Public event PropertyChangeDeventhandler Property Changed;

Private string name;

Common string name

{

Get {return name}

Setup {

Name = value;

//Trigger event

If (this. PropertyChanged! = empty)

{

This. PropertyChanged.Invoke(this,new PropertyChangedEventArgs(" Name "));

}

}

}

}

When the value of the Name property changes, the PropertyChanged event will be triggered. After receiving this event, Binding finds that the value of the property Named name has changed, so it will notify the UI element of the binding target to display the new value.

2. The display of 2.XAML interface, in which a Textbox control is used as the binding target and a Button control is used to trigger an event to change the Name property value of the Textbox.

& ltWindow x:Class="WPFBindingLearn。 Main window "

xmlns = "/winfx/2006/xaml/presentation "

xmlns:x="/winfx/2006/xaml "

title = " main window " Height = " 350 " Width = " 525 " >

& lt grid & gt

& ltStackPanel & gt

& ltTextBox x:Name = " TextBox Name " border brush = " Black " Margin = " 5 "/& gt;

& ltButton Content = " Add Age " Margin = " 5 " Click = " Button _ Click "/& gt;

& lt/stack panel & gt;

& lt/Grid & gt;

& lt/Window & gt;

3. The next step is to connect the data source and UI elements with binding.

Public partial classes main Window:windows

{

Student stu

Public main window ()

{

initialize component();

//Prepare the data source

Stu = freshman ();

//Prepare binding

Binding Binding = new Binding();

Binding. Source = stu

Binding. path = new property path(" Name ");

//Use binding to connect data from the binding target.

Binding operation. SetBinding(this.textBoxName,TextBox。 TextProperty,binding);

}

Private void Button_Click (object sender, RoutedEventArgs e)

{

Stu. Name+= " Name ";

}

}

Set the access path by setting the bound data source; Connect the data source and the target through the SetBinding () method. Three parameters of this method:

The first parameter is used to specify the target of the binding, in this case, textBoxName.

The second parameter is used to specify the property of sending data to the target.

The third parameter is to use the binding instance to associate the data source with the target.