Silverlight bidirectional binding class binding

Source: Internet
Author: User

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;
}
}
}

 

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.