私のデータ構造は次のようになります。
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 で CarList を ListView にバインドしています。Car と Driver の両方の DataTemplates を作成したいと考えています。私の UserControl は次のようになります。
<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}"/>
問題は、ListView に CarDriver の情報が表示されないことです。私は自分のラインを疑う
<UserControl DataContext="{Binding CarDriver}"/>
は間違っていますが、代わりに何を入れればよいかわかりません...?