DeChinees
DeChinees

Reputation: 293

how do I load an user control in a tabcontrol

I have a user control (UserInfo.xaml) and I want to load this my main tab control. My goal is to seprate the user control from the tab control.

I cant seem to do this in WPF

My user control looks like this.

<UserControl x:Class="AuthWiz.UserInfo"
         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" 
         d:DesignHeight="750" d:DesignWidth="1000">
<Grid>
    <Label Content="Request authorization for New User" HorizontalAlignment="Left" Margin="30,30,0,0" VerticalAlignment="Top" FontSize="20" FontWeight="Bold"/>
</Grid>

I want to load the usercontrol "userinfo" in this Tabcontrol like this.

<UserControl
         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" 
         xmlns:local="clr-namespace:AuthWiz;assembly=AuthWiz" x:Class="AuthWiz.TabControl" 
         mc:Ignorable="d" 
         d:DesignHeight="750" d:DesignWidth="1000">
<Grid>
    <TabControl HorizontalAlignment="Left" Height="677" Margin="10,63,0,0" VerticalAlignment="Top" Width="980" TabStripPlacement="Top">
        <TabItem Header="New User">
            <Grid Background="#FFE5E5E5">
                <local:UserInfo /> #this does not work.
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

I recieve the following error

The tag 'UserInfo' does not exist in XML namespace 'clr-namespace:AuthWiz;assembly=AuthWiz'.

Upvotes: 0

Views: 1231

Answers (1)

Mike Eason
Mike Eason

Reputation: 9713

TabItem can only have one child element, therefore the following is incorrect:

<TabItem Header="New User">
        <Grid Background="#FFE5E5E5"/>
        <local:UserInfo /> #this does not work.
    </TabItem>

Because your TabItem has a Grid, and your UserControl. To solve this problem, simply place your UserControl inside your Grid.

<TabItem Header="New User">
        <Grid Background="#FFE5E5E5">
           <local:UserInfo />
        </Grid>
    </TabItem>

Upvotes: 2

Related Questions