class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
Xaml中进行绑定
<Window x:Class="Wpf基础学习.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf基础学习"
Title="MainWindow" Height="350" Width="525">
<StackPanel Background="LightBlue">
<StackPanel.DataContext>
<local:Student Id="6" Age="29" Name="Time"/>
</StackPanel.DataContext>
<Grid>
<StackPanel>
<TextBox Text="{Binding Path=Id}" Margin="5" Background="Red" />
<TextBox Text="{Binding Path=Name}" Margin="5" Background="Red" />
<TextBox Text="{Binding Path=Age}" Margin="5" Background="Red" />
</StackPanel>
</Grid>
</StackPanel>
通过
xmlns:local="clr-namespace:Wpf基础学习"
可以访问类Student,从类中获得数据
这是wpf当中典型数据绑定模式。