C# 如何在tabcontrol中加载用户控件

C# 如何在tabcontrol中加载用户控件,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我有一个用户控件(UserInfo.xaml),我想把它加载到我的主选项卡控件中。 我的目标是将用户控件从选项卡控件中分离出来 我似乎不能在WPF中这样做 我的用户控件如下所示 <UserControl x:Class="AuthWiz.UserInfo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

我有一个用户控件(UserInfo.xaml),我想把它加载到我的主选项卡控件中。 我的目标是将用户控件从选项卡控件中分离出来

我似乎不能在WPF中这样做

我的用户控件如下所示

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

TabItem
只能有一个子元素,因此以下内容不正确:

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

#这是行不通的。
因为TabItem有一个网格,用户控件。要解决这个问题,只需将UserControl放在网格中即可

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


你刚刚毁了他的国际象棋;)你的回答像个笑话。他有问题!我不认为你是“专业的WPF开发者”@vercin请看关于这个问题的编辑。问题在我回答后已经改变了。@MikeEason我已经回答了。显然,这不是问题的根源。:)请在问题中详细说明什么不起作用,或者它是如何发生故障的。在这种情况下,这一点相当明显,但可能并非总是如此。我得到的错误是“标记‘UserInfo’不存在于XML命名空间‘clr namespace:AuthWiz;assembly=AuthWiz’中。”它这样说,因为它可能不存在。在assembly
AuthWiz
中的namespace
AuthWiz
中是否确实有一个名为
UserInfo
的类?顺便说一下:Mike Eason给出的解决方案对于您最初的问题是正确的。如果你还有其他问题,请把它们放在一个新问题里。
<TabItem Header="New User">
        <Grid Background="#FFE5E5E5">
           <local:UserInfo />
        </Grid>
    </TabItem>