2011-08-23 130 views
1

我正在编写一个应用程序,我在其中使用一个选项卡控件,该控件将以打开的一个选项卡开始,但允许用户打开多个其他选项卡。WPF项目模板 - TabControl

每个打开的选项卡都应该有一个树形视图,其中当用户加载文件时,我使用数据绑定填充该树形视图。

我是WPF的新手,但我觉得好像有一种方法可以创建一个包含TabItems应包含的每个元素的模板。我怎样才能使用模板做到这一点?现在我的标签项目WPF是以下

<TabItem Header="Survey 1"> 
     <TreeView Height="461" Name="treeView1" VerticalAlignment="Top" 
        Width="625" Margin="0,0,6,0" ItemTemplateSelector="{StaticResource TreeviewDataSelector}" /> 
</TabItem> 

回答

3

我想你想是这样的:

<Window x:Class="TestWpfApplication.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
<Window.Resources> 
    <DataTemplate x:Key="TabItemTemplate"> 
     <TreeView Height="461" Name="treeView1" VerticalAlignment="Top" 
        Width="625" Margin="0,0,6,0" ItemTemplateSelector="{StaticResource TreeviewDataSelector}" /> 
    </DataTemplate> 

</Window.Resources> 
<Grid> 
    <TabControl ItemsSource="{Binding ListThatPowersTheTabs}" 
       ItemTemplate="{StaticResource TabItemTemplate}"> 
    </TabControl> 
</Grid> 

你基本上创建可重用的模板,因为你提到的静态资源以他们的名字命名。

2

通常在这种情况下,我将我的TabControl.ItemsSource绑定到ObservableCollect<ViewModelBase> OpenTabs,所以我的ViewModel负责根据需要添加/删除新的选项卡。

然后如果你想在每一个标签的东西,然后覆盖TabControl.ItemTemplate并使用以下行来指定在哪里显示当前选择的TabItem

<ContentControl Content="{Binding }" />

如果您不需要设置在东西每个选项卡都不需要覆盖TabControl.ItemTemplate - 它将默认在选项卡中显示当前选中的ViewModelBase

而且我用的DataTemplates来指定要使用的查看

<DataTemplate TargetType="{x:Type local:TabAViewModel}"> 
    <local:TabAView /> 
</DataTemplate> 

<DataTemplate TargetType="{x:Type local:TabBViewModel}"> 
    <local:TabBView /> 
</DataTemplate> 

<DataTemplate TargetType="{x:Type local:TabCViewModel}"> 
    <local:TabCView /> 
</DataTemplate>