如何在 TabControl.ContentTemplate 中添加新的用户控件? [英] How to add new user control in TabControl.ContentTemplate?
问题描述
我对在 TabControl.ContentTemplate
中添加用户控件的新实例有点困难?
I am little stuck with adding new instances of a usercontrol in a TabControl.ContentTemplate
?
我的 Xaml 在这里:
My Xaml is here:
<TabControl ItemsSource="{Binding Tables}">
<TabControl.ItemTemplate>
<DataTemplate>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type uc:mytest1}">
<uc:mytest1>
</uc:mytest1>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
我将 TabControl.ItemsSource
属性绑定到一个 ObservableCollection
并且在内容模板中我添加了一个用户控件,但是当这个应用程序运行时我得到了新的项目TabItem
s 但内容页面持有相同的用户控件,但我希望为每个新的 TabItem
添加新的用户控件.
I am binding TabControl.ItemsSource
property to an ObservableCollection
and in the content template I am adding a user control, but when this app runs I am getting new items as TabItem
s but the content page is holding same user control, but I want new user controls to be added for each new TabItem
.
我对 WPF 很陌生,可能是我犯了一个非常基本的错误,请指导我.
I am very new to the WPF and may be I am doing a very basic mistake, kindly guide me.
推荐答案
ControlTemplate
确定不属于单个选项卡项的选项卡控件元素的外观.ItemTemplate
处理各个选项卡项的内容.此外,TabItem
是带标题的内容控件,这意味着它具有两个内容类型属性 Content
和 Header
以及两个单独的模板 ContentTemplate
和 HeaderTemplate
.为了能够使用绑定填充选项卡项,您需要使用上述属性设置 TabItem
的样式.
The ControlTemplate
determines the appearance of the elements of the tab control that are not part of the individual tab items. The ItemTemplate
handles the content of the individual tab items. Additionally, a TabItem
is a headered content control, which means it has two content type properties Content
and Header
with two separate templates ContentTemplate
and HeaderTemplate
. In order to be able to populate the tab items using binding, you need to style the TabItem
using the above properties.
示例:
<Window x:Class="Example.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="Window"
Title="Window2" Height="300" Width="300">
<Window.DataContext>
<Binding ElementName="Window" Path="VM"/>
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="TabItemHeaderTemplate">
<Grid>
<TextBlock Text="{Binding Header}"/>
<Ellipse Fill="Red" Width="40" Height="40" Margin="0,20,0,0"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TabItemContentTemplate">
<Ellipse Fill="Green"/>
</DataTemplate>
<Style x:Key="TabItemContainerStyle" TargetType="TabItem">
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="HeaderTemplate"
Value="{StaticResource TabItemHeaderTemplate}"/>
<Setter Property="Content" Value="{Binding}"/>
<Setter Property="ContentTemplate"
Value="{StaticResource TabItemContentTemplate}"/>
</Style>
</Window.Resources>
<Grid>
<TabControl ItemsSource="{Binding Items}"
ItemContainerStyle="{StaticResource TabItemContainerStyle}"/>
</Grid>
</Window>
背后的代码:
public partial class Window2 : Window
{
public TabControlVM VM { get; set; }
public Window2()
{
VM = new TabControlVM();
InitializeComponent();
}
}
和视图模型类:
public class TabControlVM
{
public ObservableCollection<TabItemVM> Items { get; set; }
public TabControlVM()
{
Items = new ObservableCollection<TabItemVM>();
Items.Add(new TabItemVM("tabitem1"));
Items.Add(new TabItemVM("tabitem2"));
Items.Add(new TabItemVM("tabitem3"));
Items.Add(new TabItemVM("tabitem4"));
}
}
public class TabItemVM
{
public string Header { get; set; }
public TabItemVM(string header)
{
Header = header;
}
}
这篇关于如何在 TabControl.ContentTemplate 中添加新的用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!