OK. We have already talked about control binding. If I define a user class, it has two attributes: Name and Age, so how do we bind the textbox at the front end to the attributes in this class?
Next we will talk about a simple class binding. First, we declare this class:
Using System;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Ink;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Namespace dataBind
{
Public class user
{
String name;
Public string Name
{
Get {return name ;}
Set {name = value ;}
}
Int age;
Public int Age
{
Get {return age ;}
Set {age = value ;}
}
}
}
Now I have two textbox boxes on the front-end interface to bind the class Name and Age attributes respectively, and a button to display the bound information. The interface is designed as follows:
Since textbox1 and textbox2 respectively need to bind the Name and Age attributes of the user class, I set the text values of both of them as follows:
Text = "{Binding Name, Mode = TwoWay}
Text = "{Binding Age, Mode = TwoWay}
The background code is written as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Net;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Animation;
Using System. Windows. Shapes;
Namespace dataBind
{
Public partial class MainPage: UserControl
{
Public MainPage ()
{
InitializeComponent ();
}
User u = new user ();
Private void buttonBind_Click (object sender, RoutedEventArgs e)
{
MessageBox. Show ("Name:" + u. Name + "\ n Age" + u. Age );
}
Private void LayoutRoot_Loaded (object sender, RoutedEventArgs e)
{
U. Name = "Anting ";
U. Age = 23;
TextBox1.DataContext = u;
TextBox2.DataContext = u;
}
}
}