C# WPF项目模板-选项卡控件
我正在编写一个应用程序,其中我使用一个选项卡控件,该控件将以打开一个选项卡开始,但允许用户打开多个其他选项卡 打开的每个选项卡都应该有一个树状视图,当用户加载文件时,我使用数据绑定填充该树状视图 我是WPF新手,但我觉得似乎有一种方法可以创建一个包含TabItems应该包含的每个元素的模板。如何使用模板完成此操作?现在,我对选项卡项的WPF如下C# WPF项目模板-选项卡控件,c#,.net,wpf,templates,xaml,C#,.net,Wpf,Templates,Xaml,我正在编写一个应用程序,其中我使用一个选项卡控件,该控件将以打开一个选项卡开始,但允许用户打开多个其他选项卡 打开的每个选项卡都应该有一个树状视图,当用户加载文件时,我使用数据绑定填充该树状视图 我是WPF新手,但我觉得似乎有一种方法可以创建一个包含TabItems应该包含的每个元素的模板。如何使用模板完成此操作?现在,我对选项卡项的WPF如下 <TabItem Header="Survey 1"> <TreeView Height="461" Name="tr
<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>
基本上,您可以创建可重复使用的模板作为静态资源,通过它们的键名来引用这些资源。我认为您需要这样的内容:
<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
绑定到observeCollect OpenTabs
,因此我的ViewModel负责根据需要添加/删除新选项卡
然后,如果您想在每个选项卡中显示某些内容,请覆盖TabControl.ItemTemplate
,并使用以下行指定当前所选TabItem的显示位置
如果不需要在每个选项卡中设置某些内容,则不需要覆盖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>
通常在这种情况下,我会将我的
选项卡Control.ItemsSource
绑定到一个可观察的集合OpenTabs
,因此我的ViewModel负责根据需要添加/删除新选项卡
然后,如果您想在每个选项卡中显示某些内容,请覆盖TabControl.ItemTemplate
,并使用以下行指定当前所选TabItem的显示位置
如果不需要在每个选项卡中设置某些内容,则不需要覆盖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>