当wpf在进行binding时如果要绑定的内容是dataContext中一个成员属性中成员属性时,一开始网上查了半天没找到相关东西,最后老老实实翻书才发现只要简单的A.B 就好了。哎真是用wpf的开发太少基础功不好阿。实际大体例子如下:
public class Teacher{
public string name {set;get;}
}
public class Student{
public Teacher teacher {set;get;}
public int Id {set;get;}
}
在xaml中时行binding时需要这样写
<StackPanel x:Name="container">
<TextBlock Text="学号:" />
<TextBlock Text="{Binding Id}" />
<TextBlock Text="老师:" />
<TextBlock Text="{Binding teacher.name}" />
</StackPanel>
windows类中代码:
public partial class TestBindWindow:Window
{
private Student student = new student();
public TestBindWindow(){
InitializeComponent();
this.container.DataContext =student
}
}