我的数据结构就是这样:
public class MyDataContext
{
public ObservableCollection<Car> CarList {set; get;}
}
public class Car
{
public Driver CarDriver {set; get;} // implements INotifyPropertyChanged (not shown here to keep it short)
public string Color {set; get;} // implements INotifyPropertyChanged (not shown here to keep it short)
public string Brand {set; get;} // implements INotifyPropertyChanged (not shown here to keep it short)
}
public class Driver
{
public string Name {set; get;}
public string Nationality {set; get;}
}
我对我的XAML中的一份名单持约束力,我想为汽车和司机制作数据模板。 我的用户认为:
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:Car}>
<StackPanel>
<UserControl DataContext="{Binding CarDriver}"/>
<TextBlock Text="{Binding Color}"/>
<TextBlock Text="{Binding Brand}"/>
</StackPanel>
</DataTemplate>
<DataDataTemplate DataType="{x:Type local:Driver}>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Nationality}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ListView ItemsSource="{Binding CarList}"/>
问题是,名单没有显示司机的信息。 我怀疑我的行踪。
<UserControl DataContext="{Binding CarDriver}"/>
是不正确的,但我不敢确定应该怎样做?