wpf 单独的 XAML 中的 TabItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1590031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TabItem in a separate XAML
提问by John Sheares
Is it possible to put a TabItem into a separate XAML and reference something like this:
是否可以将 TabItem 放入单独的 XAML 中并引用如下内容:
<TabControl>
<local:MyTabItem/>
</TabControl>
In Separate XAML:
<UserControl x:Class="MyProject.MyTabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TabItem Header="MyTab">
</TabItem>
</UserControl>
Of course it doesn't work, but I am wondering how can I do this?
当然它不起作用,但我想知道我该怎么做?
回答by Tony Borres
If what you want to do is simply make the code more manageable then I would recommend defining each tab's data in a user control, but still have the TabItem in the main tab control.
如果您想要做的只是使代码更易于管理,那么我建议在用户控件中定义每个选项卡的数据,但在主选项卡控件中仍然有 TabItem。
Let's assume that your original code was this:
让我们假设您的原始代码是这样的:
<TabControl>
<TabItem Header="Tab 1">
<Grid>
<TextBlock Text="Tab Data" />
</Grid>
</TabItem>
</TabControl>
To make the code more manageable you could break the tab contents into a UserControl such as:
为了使代码更易于管理,您可以将选项卡内容分解为 UserControl,例如:
<UserControl x:Class="WpfApplication19.Tab1Data"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid>
<TextBlock Text="Tab Data" />
</Grid>
</UserControl>
And then use that user control in your TabControl like this:
然后在您的 TabControl 中使用该用户控件,如下所示:
<TabControl>
<TabItem Header="Tab 1">
<tabData:Tab1Data />
</TabItem>
</TabControl>
If you really want to include the TabItem in your user control then you can do that by first creating a user control, and then change the type of the user control to the type TabItem (make sure you change this in both the xaml root node and the code behind).
如果你真的想在你的用户控件中包含 TabItem 那么你可以首先创建一个用户控件,然后将用户控件的类型更改为 TabItem 类型(确保在 xaml 根节点和后面的代码)。
This would leave you with a tab control that looks like this:
这将为您留下一个如下所示的选项卡控件:
<TabControl>
<tabData:TabItem1 />
<tabData:TabItem2 />
<tabData:TabItem3 />
</TabControl>
And each TabItem1 'User Control' would be of type TabItem. Here is an example:
每个 TabItem1 '用户控件' 都属于 TabItem 类型。下面是一个例子:
<TabItem x:Class="WpfApplication19.TabItem1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Header="Tab 1"
>
<Grid>
<TextBlock Text="Tab Data" />
</Grid>
</TabItem>
And as I mentioned, be sure to change the code behind so that it extends TabItem instead of user control:
正如我所提到的,一定要更改后面的代码,以便它扩展 TabItem 而不是用户控制:
public partial class TabItem1 : TabItem
{
public TabItem1()
{
InitializeComponent();
}
}
回答by Christian
The previous answer from Tony Borres already covers the most important aspects. But the further comment asks for access from code behind. So I will extend the example from Tony to show this aspect, too. This answer shows the required namespaces. I have added them to the answer from Tony, too.
Tony Borres 之前的回答已经涵盖了最重要的方面。但是进一步的评论要求从后面的代码访问。所以我也会扩展 Tony 的例子来展示这方面的内容。此答案显示了所需的命名空间。我也将它们添加到托尼的答案中。
To simply make the code more manageable it is recommended to define each tab's data in a user control, but still have the TabItem in the main tab control. This strategy is useful for example to work around FxCop CA1505: "Avoid unmaintainable code" when using a tab control with several tab items.
为了简单地使代码更易于管理,建议在用户控件中定义每个选项卡的数据,但在主选项卡控件中仍然有 TabItem。例如,此策略对于解决 FxCop CA1505:在使用具有多个选项卡项的选项卡控件时“避免不可维护的代码”非常有用。
Let's assume that this is the original code:
让我们假设这是原始代码:
<Window x:Class="WpfApplication19.MainWindow" ...>
<TabControl>
<TabItem Header="Tab 1">
<Grid>
<TextBlock Text="Data on Tab 1" Name="txtData1" />
</Grid>
</TabItem>
<TabItem Header="Tab 2">
<Grid>
<TextBlock Text="Data on Tab 2" Name="txtData2" />
</Grid>
</TabItem>
</TabControl>
</Window>
To make the code more manageable the tab contents can be moved into a UserControl such as:
为了使代码更易于管理,可以将选项卡内容移动到 UserControl 中,例如:
<UserControl x:Class="WpfApplication19.Tab1Data"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="OnControlLoaded"
>
<Grid>
<TextBlock Text="Data on Tab 1" Name="txtData1" />
</Grid>
</UserControl>
And then is possible to use the new user control in the TabControl like this:
然后可以像这样在 TabControl 中使用新的用户控件:
<Window x:Class="WpfApplication19.MainWindow"
xmlns:tabData="clr-namespace:WpfApplication19" ...>
<TabControl>
<TabItem Header="Tab 1">
<tabData:Tab1Data x:Name="ucTab1Data" />
</TabItem>
<TabItem Header="Tab 2">
<Grid>
<TextBlock Text="Data on Tab 2" Name="txtData2"/>
</Grid>
</TabItem>
</TabControl>
</Window>
Now it possible to access the inner wigdets of the user control from main window and vice versa. Please note the "x:" in front of the user control's name.
现在可以从主窗口访问用户控件的内部 wigdets,反之亦然。请注意用户控件名称前面的“x:”。
public partial class MainWindow : Window
{
private void AccessWidgetWithinUserControl()
{
ucTab1Data.txtData1.Text = "New text on Tab 1";
}
}
public partial class Tab1Data : UserControl
{
private MainWindow mainWindow = null; // Reference to the MainWindow
public Tab1Data()
{
InitializeComponent();
}
// get a reference to main windows when it is available.
// The Loaded Event is set in the XAML code above.
private void OnControlLoaded(object sender, RoutedEventArgs e)
{
mainWindow = Window.GetWindow(this) as MainWindow;
}
private void AccessMainWindowsWidget()
{
mainWindow.txtData2.Text = "New text on Tab 2 in the main window";
}
}
The shown code to access txtData2 would be the same even if is embedded in its own user control.
即使嵌入在其自己的用户控件中,显示的访问 txtData2 的代码也是相同的。
回答by Drew Marsh
On the surface this sounds like it would be best solved by a a style and/or template for the TabItem control which you can store in a separate resource file. How much you need to customize the actual TabItem will determine if you can just use a style or if you need a template.
从表面上看,这听起来最好通过 TabItem 控件的样式和/或模板来解决,您可以将其存储在单独的资源文件中。您需要多少自定义实际 TabItem 将决定您是否可以只使用样式或是否需要模板。
What you can do is define a named Style for each TabItem in a separate resource file like so, create a MyResources.xaml that looks something like this:
您可以做的是在单独的资源文件中为每个 TabItem 定义一个命名样式,如下所示,创建一个 MyResources.xaml,如下所示:
<ResourceDictionary>
<Style x:Key="MyTabItem" TargetType="{x:Type TabItem}">
<!--
you can just use simple property setters to set up
the TabItem or set the Template property to replace
entire tab look and feel
-->
</Style>
</ResourceDictionary>
Then in your main App.xaml file you merge in the resource dictionary:
然后在您的主 App.xaml 文件中合并资源字典:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Finally in your application you would leverage these Styles by simply doing:
最后,在您的应用程序中,您只需执行以下操作即可利用这些样式:
<TabItem Style="{DynamicResource MyTabItem}" />
回答by Jobi Joy
I think what you wanted is to get TabItem Content declare separately. Since TabItem is a ContentControl you can present a UserControl as its content.
我认为您想要的是单独声明 TabItem 内容。由于 TabItem 是一个 ContentControl,您可以将 UserControl 作为其内容呈现。
<TabControl>
<TabItem>
<local:YourTabContent1/>
</TabItem>
<TabItem>
<local:YourTabContent2/>
</TabItem>
</TabControl>
In Separate XAML:
在单独的 XAML 中:
<UserControl x:Class="MyProject.YourTabContent1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<......blah blah.../>
</UserControl>
In another XAML you can have content 2
在另一个 XAML 中,您可以拥有内容 2
<UserControl x:Class="MyProject.YourTabContent2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<......blah blah.../>
</UserControl>