简体   繁体   中英

Adding a UserControl to a TabItem

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.

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.

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.

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>

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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