[英]How to include a default set of controls inside a TabItem in WPF
I am learning WPF
, its very confusing with all the DataTemplate
stuff. 我正在学习
WPF
,它与所有DataTemplate
东西非常混乱。 I have a TabControl
and inside it I will create many TabItems
. 我有一个
TabControl
,在其中,我将创建许多TabItems
。 There are two kinds of TabItems
, ones that contain 1 textbox
, and others that contain a textbox
and a listbox
. TabItems
有两种,一种包含一个textbox
,另一种包含一个textbox
和一个listbox
。
Like so : 像这样:
I make a dozen of these tabs, in different combinations, how can I make a template/pre-made control that I can say, I want to TabControl.AddTab(style1)
and another time .Add(Style2)
... 我用不同的组合制作了许多这样的选项卡,我该如何制作一个模板/预制控件,我可以说,我想使用
TabControl.AddTab(style1)
而另外一次使用.Add(Style2)
...
Is this possible? 这可能吗? How should I go about doing it?
我应该怎么做呢?
And secondary question, if this was accomplished, how would I reference say the listbox
inside any one of the tabs if the name was auto generated? 第二个问题,如果完成了,如果名称是自动生成的,那么我将如何引用说出其中一个选项卡中的
listbox
?
Here are the two different types in XAML : 这是XAML中的两种不同类型:
<TabItem Header="Style2">
<TextBox IsReadOnly="True" TextWrapping="Wrap" />
</TabItem>
<TabItem Header="Style1">
<DockPanel>
<ListBox DockPanel.Dock="Right">
<ListBoxItem>User1</ListBoxItem>
<ListBoxItem>User2</ListBoxItem>
</ListBox>
<TextBox IsReadOnly="True" />
</DockPanel>
</TabItem>
In WPF, you can take advantage of the control's ItemTemplate (for header) and ContentTemplate (for content inside the tab) to control the content. 在WPF中,您可以利用控件的ItemTemplate(用于标题)和ContentTemplate(用于选项卡内的内容)来控制内容。
Define the dedired controls in a DataTemplate inside ContentTemplate. 在ContentTemplate内部的DataTemplate中定义删除的控件。
Here is an untested example: 这是一个未经测试的示例:
<TabControl ItemsSource="{Binding Path=AllTabs, Mode=OneWay}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=HEADERPROPERTY}" Margin="4,2,2,2" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=SOMEPROPERTY}" />
<TextBlock Text="{Binding Path=SOMEOTHERPROPERTY}" />
<TextBox Text="{Binding Path=SOMETEXTPROPERTY}" />
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
It is provided that the "AllTabs" property in the binding is an ObservableCollection containing a class which holds different properties for what you want to show. 提供的绑定中的“ AllTabs”属性是一个ObservableCollection,其中包含一个类,该类为要显示的内容保留不同的属性。 Like a String property (or more) where the Header is.
类似于Header所在的String属性(或更多)。 And whatever you need to show in the content.
以及您需要在内容中显示的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.