简体   繁体   English

将用户控件添加到TabItem

[英]Adding a UserControl to a TabItem

Originally I had my MainWindow(.xaml) that had a stackpanel and a frame. 最初,我的MainWindow(.xaml)具有一个堆栈面板和一个框架。 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. 但是,似乎因为我没有在做Web应用程序,所以使用Frame(和Pages?)并不是解决问题的正确方法。 So I changed the stackpanel and frame to a single tabcontrol (with tabs being what were the three buttons before). 因此,我将堆栈面板和框架更改为单个tabcontrol(选项卡是之前的三个按钮)。 I also changed the Pages to usercontrols. 我也将页面更改为用户控件。

However, I'm having trouble finding a way to put the Pages (now UserControls) into the content of the tabitem, without using a Frame. 但是,我很难找到一种无需使用Frame即可将Pages(现在为UserControls)放入Tabitem内容的方法。 I'm trying to do all of this within the MainWindow xaml. 我试图在MainWindow xaml中完成所有这些操作。

my MainWindow.xaml: 我的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. 尽管我对数据绑定的研究越多,但对此我也就越感到困惑。

edit: here is my BasicsPage.xaml 编辑:这是我的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>

You just need to create an instance of UserControl and put it inside TabItem . 您只需要创建一个UserControl实例并将其放在TabItem中

Say BasicsPage is your UserControl you want to put inside TabItem. BasicsPage是您要放入TabItem的UserControl。 All you have to do this: 您要做的所有事情:

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

Define local namespace at root window where BasicsPage is defined in something like: 在类似于BasicsPage的根窗口中定义本地名称空间:

<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM