您的位置:首页 > 其它

WPF 使用DataContext 作为 Binding的源

2011-10-17 10:11 253 查看
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当中典型数据绑定模式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: