2012-01-13 88 views
0

我想绑定一个属性ObservableCollection<ObservableCollection<Location>>列表框与ListBox的ItemTemplate反过来有一个网格的ItemTemplate。列表框中的列表框的布局似乎工作正常。但是,我的数据绑定存在问题。列表框的列表框绑定到集合的集合

对于第二层列表框ItemsSource,我尝试使用集合当前项目绑定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

为什么你需要一个包含ListBoxes的ListBox? – Bernard 2012-01-13 16:36:37

回答

1

每个ListBoxItem包含无论对象是父集合在DataContext,所以你应该罚款,指定没有在绑定。

<ListBox ItemsSource="{Binding MyCollectionOfCollections}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <!-- DataContext of each item will an inner ObservableCollection<Location> --> 
      <ListBox ItemsSource="{Binding }" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

来排列答案。我一直在脑海中陷入困境,我需要再往下一级才能进入第二名单。感谢您指出了这一点! – scuba88 2012-01-13 17:45:40

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 2012-01-13 17:47:02

+0

不,我的速度快了大约28秒(并且在这个网站上刚刚发现了另一个有用的功能)......但是这一次,我会让她脱身;) – Nuffin 2012-01-13 18:07:02

+0

@Tobias你比我快了28秒,所以来自我的+1 :)我认为如果我刚刚发布了代码示例,我会更快,但我想包括解释为什么绑定能够工作,所以不得不想一分钟。 – Rachel 2012-01-13 18:15:03

相关问题