绑定TabItem的内容控件 [英] Binding for TabItem's content controls

查看:781
本文介绍了绑定TabItem的内容控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabControl,其ItemsSource设置为 ObservableCollection< BookTab> 并使用ContentTemplateSelector创建不同的选项卡。

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>

键入BookTab确定在相应选项卡中使用的DataTemplate,名称显示在选项卡标题和数据应该显示在标签的内容,即DataGrid。
数据设置为不同类型的ObservableCollections。

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可能如下所示:

DataTemplate may look like this:

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

我尝试过不同的方式将Data属性绑定到DataGrid的ItemsSource,但是我所有的都是网格显示单词书(BookTab的名称属性值)。
我的猜测是我以某种方式传播TabControl的绑定到DataGrid,但我无法理解。

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.

推荐答案

我会这样做:

<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>

...稍后您在app.xaml中定义了数据的内容是如何呈现的。 ..

... 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>

所有您需要做的是为每种类型创建一个视图(usercontrol)。

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

HTH

这篇关于绑定TabItem的内容控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆