当我创建一个资源时,我们在其中指定数据类型:
<Window.Resources>
<DataTemplate x:Key="StudentView"
DataType="this:StudentData">
<TextBox Text="{Binding Path=StudentFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1"
Grid.Column="2"
VerticalAlignment="Center" />
<TextBox Text="{Binding Path=StudentGradePointAverage}"
Grid.Row="2"
Grid.Column="2"
VerticalAlignment="Center" />
</DataTemplate>
<Window.Resources>
同时,在绑定时:
<ItemsControl ItemsSource="{Binding TheStudents}"
ItemTemplate="{StaticResource StudentView}">
那么为什么我们要使用 DataType,即使我去掉 DataType,我的示例也可以正常运行。它是否限制了某些类型,可以放在 DataTemplate 中?
但是我尝试将其中一个文本框绑定到垃圾值(不在视图模型中)上,它也可以正常工作!