Binding for TabItem's content controls

可紊 提交于 2019-12-12 06:01:24

问题


I have a TabControl with ItemsSource set to ObservableCollection<BookTab> and using ContentTemplateSelector to create different tabs.

class BookTab
{  
    public string Name { get; set; }  
    public string Type { get; set; }  
    public object Data { get; set; }  
}

<TabControl Name="tabControl"
            ContentTemplateSelector="{StaticResource tabTemplateSelector}">  
    <TabControl.ItemContainerStyle>  
        <Style TargetType="TabItem">  
            <Setter Property="Header" Value="{Binding Name}"/>  
            <Setter Property="Content" Value="{Binding}"/>  
        </Style>  
    </TabControl.ItemContainerStyle>  
</TabControl>

Type in BookTab determines DataTemplate used in the appropriate tab, Name is displayed on the tab header, and Data supposed to be displayed in tab's content, i.e. DataGrid. Data is set to ObservableCollections of different types.

DataTemplate may look like this:

<DataTemplate x:Key="bookTabTemplate">  
    <TabItem Name="bookTab">  
        <Grid>  
            <DataGrid Name="bookGrid">  
                ...  
            </DataGrid>  
        </Grid>  
    </TabItem>  
</DataTemplate>

I tried different ways to bind Data property to DataGrid's ItemsSource, but all I got is grid displaying word "Book" (BookTab's Name property value). My guess is I have to somehow propagate TabControl's binding down to DataGrid, but I cannot figure it out.


回答1:


I would do it like this:

<TabControl SelectedItem="{Binding CurrentBook}"
          IsSynchronizedWithCurrentItem="True"
          ItemsSource="{Binding BookList}">
<TabControl.ContentTemplate>
  <DataTemplate>
     <Grid>
        <ContentControl Content="{Binding Data}"
     </Grid>
  </DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemTemplate>
  <DataTemplate>
    <TextBlock Text="{Binding Name}"/>
  </DataTemplate>
</TabControl.ItemTemplate>
</TabControl>

... and later you define in your app.xaml how the content of your data is presented...

    <DataTemplate DataType="{x:Type viewmodel:bookviewmodel1}">
        <view:bookview1/>
    </DataTemplate>

All you have to do, is creating a view (usercontrol) for each type.

HTH




回答2:


your datagrid data context is probably the BookTab (you can confirm this using snoop )

If this is correct, all you have to do is bind the datagrid itemssource to the BookTab Data property

<DataGrid Name="bookGrid" ItemsSource="{Binding Path=Data}" />

This should do the trick



来源:https://stackoverflow.com/questions/4206244/binding-for-tabitems-content-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!