windows8应用开发——数据绑定
1、单条数据绑定
①建立数据类
public class Employee { public string Name { get; set; } public int Number { get; set; } public string Post { get; set; } public Double Salary { get; set; } public Employee(string name, int number, string post, Double salary) { Name = name; Number = number; Post = post; Salary = salary; } }
②前台文本框
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="LayoutRoot"> <TextBlock HorizontalAlignment="Left" Margin="441,131,0,0" TextWrapping="Wrap" Text="员工信息表" VerticalAlignment="Top" Height="60" FontSize="40"/> <TextBlock HorizontalAlignment="Left" Margin="352,221,0,0" TextWrapping="Wrap" Text="姓名:" VerticalAlignment="Top" FontSize="30" Height="60" Width="104"/> <TextBlock HorizontalAlignment="Left" Margin="352,286,0,0" TextWrapping="Wrap" Text="工号:" VerticalAlignment="Top" Width="90" Height="60" FontSize="30" RenderTransformOrigin="0.311,0.441"/> <TextBlock HorizontalAlignment="Left" Margin="352,346,0,0" TextWrapping="Wrap" Text="职务:" VerticalAlignment="Top" Width="90" Height="60" FontSize="30"/> <TextBlock HorizontalAlignment="Left" Margin="352,411,0,0" TextWrapping="Wrap" Text="薪资:" VerticalAlignment="Top" FontSize="30" Width="90" Height="60"/> <TextBox x:Name="NameTxb" HorizontalAlignment="Left" Margin="524,221,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" FontSize="25" Height="40"/> <TextBox x:Name="NumberTxb" HorizontalAlignment="Left" Margin="524,286,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="25" Width="200" Height="40" RenderTransformOrigin="1.075,0.363"/> <TextBox x:Name="PostTxb" HorizontalAlignment="Left" Margin="524,346,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Height="40" FontSize="25"/> <TextBox x:Name="SalaryTxb" HorizontalAlignment="Left" Margin="524,411,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Height="40" FontSize="25"/> </Grid>
③后台绑定
InitializeComponent(); Employee employee = new Employee("小亮", 001, "经理", 15000); LayoutRoot.DataContext = employee; //绑定姓名 Binding bindingName = new Binding(); //设置绑定模式 bindingName.Mode = BindingMode.OneWay; bindingName.Path = new PropertyPath("Name"); NameTxb.SetBinding(TextBox.TextProperty, bindingName); //绑定工号 Binding bindingNumber = new Binding(); bindingNumber.Mode = BindingMode.OneWay; bindingNumber.Path = new PropertyPath("Number"); NumberTxb.SetBinding(TextBox.TextProperty, bindingNumber); //绑定职务 Binding bindingPost = new Binding(); bindingPost.Mode = BindingMode.OneWay; bindingPost.Path = new PropertyPath("Post"); PostTxb.SetBinding(TextBox.TextProperty, bindingPost); //绑定薪资 Binding bindingSalary = new Binding(); bindingSalary.Mode = BindingMode.OneWay; bindingSalary.Path = new PropertyPath("Salary"); SalaryTxb.SetBinding(TextBox.TextProperty, bindingSalary);
2、集合对象绑定
①前台布局
<ComboBox x:Name="ShowComboBox" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="219,205,0,513" VerticalAlignment="Top" Width="380" FontSize="30" Height="50"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="姓名" FontSize="25" /> <TextBlock Text="{Binding Name}" FontSize="25" /> <TextBlock Text="年龄" FontSize="25" /> <TextBlock Text="{Binding Age}" FontSize="25" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <Button Width="350" Height="60" Margin="638,293,0,415" FontSize="30" Content="参加集合数据" Click="Button_Click_1" /> <Button Width="350" Height="60" Margin="638,218,0,490" FontSize="30" Content="修改Person对象数据" Click="Button_Click_2" />
②后台创建person类
public class Person : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public Person(string name, int age) { Name = name; Age = age; } private string _name; private int _age; public string Name { get { return _name; } set { _name = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } public int Age { get { return _age; } set { _age = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Age")); } } } }
③初始化数据
public ObservableCollection<Person> personList = new ObservableCollection<Person>(); public MainPage() { this.InitializeComponent(); personList.Add(new Person("Jack", 25)); personList.Add(new Person("Tom", 10)); personList.Add(new Person("Amy", 29)); ShowComboBox.DataContext = personList; }
④实现添加修改功能
private void Button_Click_1(object sender, RoutedEventArgs e) { personList.Add(new Person("Tim", 30)); } private void Button_Click_2(object sender, RoutedEventArgs e) { personList[0].Name = "Json"; personList[0].Age = 20; }
3、分层数据绑定
①声明数据资源实例
<Page.Resources> <local:SchoolList x:Key="SchoolData"/> <CollectionViewSource x:Name="School" Source="{StaticResource SchoolData}"/> <CollectionViewSource x:Name="Grades" Source="{Binding Grades,Source={StaticResource School}}"/> <CollectionViewSource x:Name="Classes" Source="{Binding Classes,Source={StaticResource Grades}}"/> </Page.Resources>
②前台显示
<ListBox ItemsSource="{Binding Source={StaticResource School}}" DisplayMemberPath="Name" Margin="100,50,0,766"/> <ListBox ItemsSource="{Binding Source={StaticResource Grades}}" DisplayMemberPath="Name" Margin="0,50,0,725"/> <ListBox ItemsSource="{Binding Source={StaticResource Classes}}" DisplayMemberPath="Name" Margin="0,50,0,685"/> <ContentControl Content="{Binding Source={StaticResource Classes}}" Margin="22,88,-22,-38"> <ContentControl.ContentTemplate> <DataTemplate> <StackPanel Margin="5"> <StackPanel Orientation="Horizontal" Margin="10,10"> <TextBlock Text="男生人数:" Margin="0,0,5,0" FontSize="20"/> <TextBlock Text="{Binding BoyNum}" FontSize="20"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="10,0"> <TextBlock Text="女生人数:" Margin="0,0,5,0" FontSize="20"/> <TextBlock Text="{Binding GirlNum}" FontSize="20"/> </StackPanel> </StackPanel> </DataTemplate> </ContentControl.ContentTemplate> </ContentControl>
③新建三个数据类
public class Class { public string Name { get; set; } public int BoyNum { get; set; } public int GirlNum { get; set; } } public class Grade { public string Name { get; set; } public IEnumerable<Class> Classes { get; set; } } public class School { public string Name { get; set; } public IEnumerable<Grade> Grades { get; set; } }
④实现分级数据初始化
public class SchoolList : List<School> { public SchoolList() { this.AddRange(GetSchool()); } public IEnumerable<School> GetSchool() { return from x in Enumerable.Range(1, 2) select new School { Name = "学校" + x, Grades = GetGrades(x) }; } public IEnumerable<Grade> GetGrades(int x) { return from y in Enumerable.Range(1, 3) select new Grade { Name = String.Format("年级{0}-{1}", x, y), Classes = GetClasses(x, y) }; } public IEnumerable<Class> GetClasses(int x, int y) { return from z in Enumerable.Range(1, 4) select new Class { Name = String.Format("班级{0}-{1}-{2}", x, y, z), BoyNum = 25 - (x * y * z), GirlNum = x * y * z }; } }