程序集整體框架如下
MainWindow前台代碼中Text="{Binding Stu.Name}", Text="{Binding Stu.Gender}" ,Text="{Binding Stu.Age}"都是綁定到類成員

<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid x:Name="GuanJiaoGrid"> <Grid.DataContext> <local:Students></local:Students> </Grid.DataContext> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="40"/> <RowDefinition Height="40"/> <RowDefinition Height="40"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" FontSize="22" >名字</Label> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="2" VerticalAlignment="Center" FontSize="22" >性別</Label> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="3" VerticalAlignment="Center" FontSize="22" >年齡</Label> <TextBox Text="{Binding Stu.Name}" Grid.Column="1" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu.Gender}" Grid.Column="2" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu.Age}" Grid.Column="3" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> </Grid> </Window>
MainWindow后台代碼什么也不寫,Students類如下

using System; using System.ComponentModel; using System.Runtime.CompilerServices; namespace WpfApp1 { public class Students { public Student Stu { get; set; } = new Student(); public Students() { Stu.Name = "李明"; Stu.Gender = "男"; Stu.Age = "15"; } } #region 學生類 public class Student : INotifyPropertyChanged { private string _name; public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged(); } } private string _gender; public string Gender { get { return _gender; } set { _gender = value; NotifyPropertyChanged(); } } private string _age; public string Age { get { return _age; } set { _age = value; NotifyPropertyChanged(); } } #region 屬性更改 public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } #endregion }
運行結果如下
當有多個學生信息的時候,可以綁定到集合類,Students類做一下修改

using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; namespace WpfApp1 { public class Students { public ObservableCollection<Student> Stu { get; set; } = new ObservableCollection<Student>();//創建集合類 public Students()//構造方法添加集合類成員 { Stu.Add(new Student("李明","男","15")); Stu.Add(new Student("李玟","女","14")); } } #region 學生類 public class Student : INotifyPropertyChanged { private string _name; public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged(); } } private string _gender; public string Gender { get { return _gender; } set { _gender = value; NotifyPropertyChanged(); } } private string _age; public string Age { get { return _age; } set { _age = value; NotifyPropertyChanged(); } } public Student(string name,string gender,string age) { Name = name; Gender = gender; Age = age; } #region 屬性更改 public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } #endregion }
MainWindow前台代碼中Text="{Binding Stu[0].Name}", Text="{Binding Stu[0].Gender}" ,Text="{Binding Stu[0].Age}"都是綁定到集合類成員

<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid x:Name="GuanJiaoGrid"> <Grid.DataContext> <local:Students></local:Students> </Grid.DataContext> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="40"/> <RowDefinition Height="40"/> <RowDefinition Height="40"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="1" VerticalAlignment="Center" FontSize="22" >名字</Label> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="2" VerticalAlignment="Center" FontSize="22" >性別</Label> <Label Grid.Row="0" HorizontalAlignment="Center" Grid.Column="3" VerticalAlignment="Center" FontSize="22" >年齡</Label> <TextBox Text="{Binding Stu[0].Name}" Grid.Column="1" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu[0].Gender}" Grid.Column="2" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu[0].Age}" Grid.Column="3" Grid.Row="1" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu[1].Name}" Grid.Column="1" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu[1].Gender}" Grid.Column="2" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox> <TextBox Text="{Binding Stu[1].Age}" Grid.Column="3" Grid.Row="2" TextAlignment="Center" FontSize="22" ></TextBox> </Grid> </Window>
當然收集學生信息這種事情最好是用DataGrid控件去做,這里只是介紹一下前台控件成員如何綁定到類成員以及集合類成員的使用。