繁体   English   中英

WPF项目模板-TabControl

[英]WPF Item Templates - 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>

我想你想要这样的东西:

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

基本上,您将可重用模板创建为静态资源,并通过它们的键名进行引用。

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

然后,如果要在每个Tab中添加某些内容,则覆盖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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM