2014-02-22 93 views
0

我想設計一個tabcontrol。每個tabitem都有一個網格。可以說網格是數據綁定到對象列表。現在我該如何綁定tabcontrol。tabcontol的數據綁定-wpf

下面顯示的是類

Class GridData 
{ 
    String Name; 
    string Id; 
    string location 
} 

Class TabItems 
{ 
    string tabName; 
    List<GridData> ls; 
} 

請告訴我實現數據綁定

+0

這個問題對我而言並不清楚。你想要將TabControl的'ItemsSource'綁定到'TabItems'的list/observablecollection? – har07

回答

1

的最好辦法說你有這2類:

public class GridData 
{ 
    public string Name { get; set; } 
    public string Id { get; set; } 
    public string Location { get; set; } 
} 

public class TabItems 
{ 
    public string tabName { get; set; } 
    public List<GridData> ls { get; set; } 
} 

並設置TabControl.ItemsSourceList<TabItems>那麼你XAML的TabControl可以是這樣的:

<TabControl> 
    <TabControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding tabName}"/> 
     </DataTemplate> 
    </TabControl.ItemTemplate> 
    <TabControl.ContentTemplate> 
     <DataTemplate> 
      <ItemsControl ItemsSource="{Binding ls}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Path=Id, StringFormat='{}Id: {0}'}"/> 
          <TextBlock Text="{Binding Path=Name, StringFormat='{}Name: {0}'}"/> 
          <TextBlock Text="{Binding Path=Location, StringFormat='{}Location: {0}'}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </DataTemplate> 
    </TabControl.ContentTemplate> 
</TabControl> 

指定2 DataTemplatesTabControl.ItemTemplate代表標題,TabControl.ContentTemplate代表內容,在本例中爲ItemsControl顯示ls