2012-01-13 6 views
0

プロパティObservableCollection<ObservableCollection<Location>>をListBoxのItemTemplateを持つListBoxにバインドしようとしていますが、ListBoxのItemTemplateにはグリッドのItemTemplateがあります。リストボックス内のリストボックスのレイアウトはうまくいくようです。しかし、私はデータバインディングに問題があります。コレクションのコレクションにバインドされたリストボックスのリストボックス

2番目のレイヤーリストボックスのItemsSource私は、Collection Current Item Bindings ItemSource="{Binding /}"を使用し、ItemsSource="{TemplateBinding /}"を使用してバインディングを試行しました。私はWPFを初めて使い、MVVMを使っていますので、ヒントや批評をいただければ幸いです。

<ListBox Grid.Row="4" Width="610" Height="600" HorizontalContentAlignment="Stretch" ItemsSource="{Binding CurrentLocation.Children}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <ListBox Width="550" Height="100" Margin="5" HorizontalContentAlignment="Stretch" ItemsSource="{Binding /}" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Grid HorizontalAlignment="Stretch" Margin="5"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*" /> 
           <ColumnDefinition Width="3*" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="*" /> 
           <RowDefinition Height="*" /> 
          </Grid.RowDefinitions> 
          <TextBlock Grid.Column="0" Grid.Row="0" Margin="5" Text="Name:" /> 
          <TextBlock Grid.Column="0" Grid.Row="1" Margin="5" Text="Description:" /> 
          <TextBlock Grid.Column="1" Grid.Row="0" Margin="5" Text="{Binding Name}" /> 
          <TextBlock Grid.Column="1" Grid.Row="1" Margin="5" Text="{Binding Description}" TextWrapping="Wrap" /> 
          <Button Grid.Column="2" Grid.Row="0" Command="{Binding TODO}"> 
           <TextBlock Text="Edit"/> 
          </Button> 
          <Button Grid.Column="2" Grid.Row="1" Command="{Binding TODO}"> 
           <TextBlock Text="Delete"/> 
          </Button> 
         </Grid> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

なぜリストボックスを含むリストボックスが必要ですか? – Bernard

答えて

1

ListBoxItemは、オブジェクトが親コレクションにあるもののDataContext含まれているので、あなたは、バインディングでは何も指定しない場合は問題ないはずです。

+0

ああ、そうです。私はそれが私の頭の中に止まっていたので、その2番目のリストに行くためにもう一度レベルを下げる必要があった。これを指摘してくれてありがとう! – scuba88

1

全体として現在DataContextへの結合のための正しい表記は{Binding}あります。

<ListBox ... ItemsSource="{Binding CurrentLocation.Children}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <ListBox ... ItemsSource="{Binding}" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Grid HorizontalAlignment="Stretch" Margin="5"> 
          <!-- snip --> 
         </Grid> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

私はレイチェルがあなたにそれを打つと思う。ありがとう、結構です! – scuba88

+0

いいえ、私は約28秒速かった(とちょうどこのサイトでもう一つの便利な機能を発見した)...しかし、今回は私は彼女がそれを取り除くようになります;) – Nuffin

+0

@Tobiasあなたは私より28秒早かったので私から+1:コードサンプルを投稿したほうが速かったと思いますが、なぜバインディングが機能したのか説明したかったので、分を考えなければなりませんでした。 – Rachel

関連する問題