the Data Form in Silverlight

1. Create data entity

public class Person:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string firstName; private string lastName; private int age; public string FirstName { get { return firstName; } set { firstName = value; OnPropertyChanged("FirstName"); } } public string LastName { get { return lastName; } set { lastName = value; OnPropertyChanged("LastName"); } } public int Age { get { return age; } set { age = value; OnPropertyChanged("Age"); } } private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }

2. Define the dataform control, before doing this, you have to add the reference in the application

System.Windows.Controls.Data.DataForm.Toolkit

 

 

3. this is the code behind

public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { this.DataContext = this; } ObservableCollection people; public ObservableCollection People { get { if (people == null) { people = new ObservableCollection(); people.Add(new Person(){ FirstName="Fred", LastName="Smith", Age=21}); people.Add(new Person() { FirstName = "Michael", LastName = "Jackson", Age = 50 }); people.Add(new Person() { FirstName = "David", LastName = "Backham", Age = 30 }); } return people; } } }

 

 

你可能感兴趣的:(WPF/SilverLight)