将 UserControl 添加到 TabItem [英] Adding a UserControl to a TabItem

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

问题描述

最初我的 MainWindow(.xaml) 有一个堆栈面板和一个框架.堆栈面板中有三个导航按钮,框架具有三个页面之一(基于用户单击的导航按钮).然而,似乎因为我不是在做一个网络应用程序,所以使用 Frame(和 Pages?)并不是正确的方法.所以我将堆栈面板和框架更改为单个选项卡控件(选项卡是之前的三个按钮).我还将页面更改为用户控件.

Originally I had my MainWindow(.xaml) that had a stackpanel and a frame. Within the stackpanel were three navigation buttons and the frame had one of the three Pages (based on which navigation button the user clicked). However, it seems that since I'm not doing a web app, that using Frame (and Pages?) is not the right way to go about it. So I changed the stackpanel and frame to a single tabcontrol (with tabs being what were the three buttons before). I also changed the Pages to usercontrols.

但是,我在不使用框架的情况下找不到将页面(现在是用户控件)放入 tabitem 的内容的方法.我正在尝试在 MainWindow xaml 中完成所有这些操作.

However, I'm having trouble finding a way to put the Pages (now UserControls) into the content of the tabitem, without using a Frame. I'm trying to do all of this within the MainWindow xaml.

我的 MainWindow.xaml:

my MainWindow.xaml:

<Window x:Class="ConstructedLanguageOrganizerTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="454" Width="573">
    <Grid>
        <TabControl HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Name="tabControl1">
            <TabItem Header="Basics" Name="basicsTab">
                //What can I use here instead of Frame?
            </TabItem>

            <TabItem Header="Words" Name="wordsTab">
                <Grid>
                    <Frame Source="WordsPage.xaml"/>
                </Grid>
            </TabItem>

            ...
        </TabControl>
    </Grid>
</Window>

我是不是用错了方法?我想我应该使用某种数据绑定,也许吧?虽然,我对数据绑定的研究越多,我也越对此感到困惑.

Am I going about this the wrong way? I think that I'm suppose to use some sort of databinding, maybe? Although, the more I look at things on data binging, the more I just get confused on that as well.

这是我的 BasicsPage.xaml

edit: here is my BasicsPage.xaml

<UserControl x:Class="ConstructedLanguageOrganizerTool.BasicsPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" x:Name="basicsPage" Height="349" Width="334">

    <Grid>
        // Grid Row and Column defs here

        //Number of textboxs and textblocks here.

    </Grid>
</UserControl>

推荐答案

您只需要创建一个 UserControl 实例并将其放入 TabItem.

You just need to create an instance of UserControl and put it inside TabItem.

BasicsPage 是您想要放入 TabItem 的 UserControl.您所要做的就是:

Say BasicsPage is your UserControl you want to put inside TabItem. All you have to do this:

<TabItem Header="Basics" Name="basicsTab">
   <local:BasicsPage/>
</TabItem>

在根窗口定义本地命名空间,其中 BasicsPage 定义如下:

Define local namespace at root window where BasicsPage is defined in something like:

<Window x:Class="ConstructedLanguageOrganizerTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ConstructedLanguageOrganizerTool"> <-- HERE

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

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