2013-10-06 49 views
0

而不是聲明爲每個ObservableCollections的標籤明確地如在第一TabControl的下面我需要他們如在第二的TabControl動態生成和具有嵌套的ListView組的的ItemsSource到每個嵌套的ObservableCollections。ObservableCollections嵌套的ListView中的TabControl的結合的ObservableCollection的TabItems

換句話說:爲什麼第二個TabControl中的嵌套ListViews的ItemSource綁定不起作用?有沒有辦法將嵌套的ObservableCollection的索引設置爲包含ObservableCollection的索引?

或者:我如何使第二個動態TabControl和嵌套ListViews看起來像第一個靜態TabControl和嵌套ListViews?

using System.Collections.ObjectModel; 
using System.Windows; 

namespace GridViewColumns2 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      ViewModel viewModel = new ViewModel(); 

      viewModel.ThingCollections = new ObservableCollection<ThingCollection>(); 

      viewModel.ThingCollections.Add(new ThingCollection { Name = "One" }); 
      viewModel.ThingCollections[0].Things = new ObservableCollection<Thing>(); 
      viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.One" }); 
      viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Two" }); 
      viewModel.ThingCollections[0].Things.Add(new Thing { Name = "One.Three" }); 


      viewModel.ThingCollections.Add(new ThingCollection { Name = "Two" }); 
      viewModel.ThingCollections[1].Things = new ObservableCollection<Thing>(); 
      viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.One " }); 
      viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Two" }); 
      viewModel.ThingCollections[1].Things.Add(new Thing { Name = "Two.Three" }); 

      DataContext = viewModel; 
     } 
    } 

    public class ViewModel 
    { 
     public ObservableCollection<ThingCollection> ThingCollections { get; set; } 
    } 

    public class ThingCollection 
    { 
     public string Name { get; set; } 

     public ObservableCollection<Thing> Things { get; set; } 
    } 

    public class Thing 
    { 
     public string Name { get; set; } 
    } 
} 



<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <TabControl> 
     <TabItem> 
      <TabItem.Header>One</TabItem.Header> 
      <TabItem.Content> 
       <ListView ItemsSource="{Binding Path=ThingCollections[0].Things}"> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" /> 
         </GridView> 
        </ListView.View> 
       </ListView> 
      </TabItem.Content> 
     </TabItem> 
     <TabItem> 
      <TabItem.Header>Two</TabItem.Header> 
      <TabItem.Content> 
       <ListView ItemsSource="{Binding Path=ThingCollections[1].Things}"> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" /> 
         </GridView> 
        </ListView.View> 
       </ListView> 
      </TabItem.Content> 
     </TabItem> 
    </TabControl> 

    <TabControl Grid.Column="1" ItemsSource="{Binding Path=ThingCollections}"> 
     <TabControl.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </TabControl.ItemTemplate> 
     <TabControl.ContentTemplate> 
      <DataTemplate> 
       <ListView ItemsSource="{Binding Path=ThingCollections[0].Things}"> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Width="120" Header="Name" DisplayMemberBinding="{Binding Name}" /> 
         </GridView> 
        </ListView.View> 
       </ListView> 
      </DataTemplate> 
     </TabControl.ContentTemplate> 
    </TabControl> 


</Grid> 

回答

1

當你在一個DataTemplateDataContext已經是該項目的(在你的情況下,ThingCollection

所以不是:

<ListView ItemsSource="{Binding Path=ThingCollections[0].Things}"> 

用途:

<ListView ItemsSource="{Binding Path=Things}"> 

要診斷未來的綁定錯誤,請按F5(開始調試)然後在輸出窗口中查看。所有綁定錯誤將被記錄在那裏。在這種情況下,它會表示它在對象ThingCollection中找不到名爲ThingCollections的屬性。

0

在BindingPath中刪除ThingCollections[0].DataContext將被設置爲您正確的ThingCollection。

<ListView ItemsSource="{Binding Path=Things}"> 
相關問題