2012-05-03 28 views
0

テンプレートを作成し、リスト項目のプロパティ内のオブジェクトへの結合:WPFは私のデータ構造は次のようになり

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でのListViewにCarListを結合していると私はDataTemplatesを作成したいですカーとドライバーの両方で使用できます。私のユーザーコントロールは、次のようになります。

<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}"/> 

問題は、リストビューがCarDriverの情報を表示しないことです。私は私の行を疑う

<UserControl DataContext="{Binding CarDriver}"/> 

は間違っていますが、代わりに何を入れるべきかわかりません...?

+0

おそらく問題はあなたのUserControlにあります。正しい方法でDataContextを使用していますか? [再利用可能なUserControlsを作成するためのシンプルなパターン](http://www.scottlogic.co.uk/blog/colin/2012/02/a-simple-pattern-for-creating-re-useable- usercontrols-in-wpf-silverlight /)を使用します。 – LPL

答えて

1

あなたはこのようContentControlを置く必要があります。

<ContentControl Content="{Binding CarDriver}"> 

、すべてが正常に動作します。そうでない場合 - DataTemplateDriverの場合DataTemplateの場合Car

関連する問題