单独的 XAML 中的 TabItem [英] TabItem in a separate XAML

查看:30
本文介绍了单独的 XAML 中的 TabItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 TabItem 放入单独的 XAML 并引用如下内容:

Is it possible to put a TabItem into a separate XAML and reference something like this:

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

推荐答案

如果您只想使代码更易于管理,那么我建议您在用户控件中定义每个选项卡的数据,但仍将 TabItem 放在主选项卡控件.

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.

假设您的原始代码是这样的:

Let's assume that your original code was this:

<TabControl>
    <TabItem Header="Tab 1">
        <Grid>
            <TextBlock Text="Tab Data" />
        </Grid>
    </TabItem>
</TabControl>

为了使代码更易于管理,您可以将选项卡内容分解为 UserControl,例如:

To make the code more manageable you could break the tab contents into a UserControl such as:

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

然后像这样在 TabControl 中使用该用户控件:

And then use that user control in your TabControl like this:

    <TabControl>
        <TabItem Header="Tab 1">
            <tabData:Tab1Data />
        </TabItem>
    </TabControl>

如果你真的想在你的用户控件中包含 TabItem,那么你可以先创建一个用户控件,然后将用户控件的类型更改为 TabItem 类型(确保在 xaml根节点和后面的代码).

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

这会给您留下一个如下所示的选项卡控件:

This would leave you with a tab control that looks like this:

    <TabControl>
        <tabData:TabItem1 />
        <tabData:TabItem2 />
        <tabData:TabItem3 />
    </TabControl>

并且每个 TabItem1用户控件"都属于 TabItem 类型.下面是一个例子:

And each TabItem1 'User Control' would be of type TabItem. Here is an example:

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

正如我所提到的,一定要更改后面的代码,以便它扩展 TabItem 而不是用户控制:

And as I mentioned, be sure to change the code behind so that it extends TabItem instead of user control:

public partial class TabItem1 : TabItem
{
    public TabItem1()
    {
        InitializeComponent();
    }
}

这篇关于单独的 XAML 中的 TabItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆