首页 > 解决方案 > 将 TabControl 放在 Grid 内会出现错误

问题描述

在 WPF 中创建窗口时,尝试将 TabControl 放在 Grid 中时会出现以下错误:

错误 1 ​​属性元素不能位于元素内容的中间。它们必须在内容之前或之后。

开发平台:Windows-7:Visual Studio 2008

XAML 代码:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="USBTOOL"
Title="Window1"
Height="768" Width="1300" ResizeMode="NoResize">

 <UserControl>
   <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Border BorderThickness="1" CornerRadius="4,4,4,4" BorderBrush="#FF8C8E94" Background="#8C208AB0" Margin="8,20.66,10,0" Height="51" VerticalAlignment="Top" TextElement.FontWeight="Bold" TextElement.FontFamily="Comic Sans MS" TextElement.FontSize="18" TextElement.Foreground="#FF000000" OpacityMask="{x:Null}" x:Name="TopTitleMenu"/>
        <Border BorderThickness="1"
        CornerRadius="6,6,6,6" BorderBrush="#FF8C8E94" Background="#8C208AB0" Margin="8,76,10,10" TextElement.FontWeight="Bold" TextElement.FontFamily="Comic Sans MS" TextElement.FontSize="18" TextElement.Foreground="#FF000000" OpacityMask="{x:Null}"/>


    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="-0.072,-0.118">
            <GradientStop Color="#FF132734" Offset="0"/>
            <GradientStop Color="#FF072E42" Offset="1"/>
        </LinearGradientBrush>
    </Grid.Background>

        <TabControl>  </TabControl>

  </Grid>
 </UserControl>
</Window>

标签: c#wpfxamluser-interface

解决方案


TabControl 不是问题。问题是 <Grid.Background> ... </Grid.Background>

像这样为 Grid 设置属性必须在 Grid 控件的所有内容之前或之后进行。

只需将 TabControl 声明移到 Grid.Background 上方即可。


推荐阅读