XAML - 属性 'Content' 被设置了多次

54

我对WPF和XAML非常陌生。我无法理解为什么在以下代码中我不能将WPF控件放置在我想要的位置。我的问题在于<canvas></canvas>标记的位置。我放置在此处的任何内容都会给我带来“属性'Content'设置多次”的错误。

如果有人能够用简单的语言解释设置Content属性的位置,那将是最有帮助的。

我已经查看了以下文章,但都没有成功: the property 'Content' is set more than once the property content is set more than once Property content is set more than once The property 'Content' is set more than once Button WPF ControlTemplate causeing error "The property 'content' is set more than once"

<Window x:Class="PDFIndexer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="ParentGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>
    <Menu Grid.Row="0" >
        <MenuItem Header="File" >
            <MenuItem Header="Open Project" Click="MenuItem_Click_1"></MenuItem>
            <MenuItem Header="Save Project"></MenuItem>
            <MenuItem Header="Close Project"></MenuItem>
            <Separator></Separator>
            <MenuItem Header="Exit"></MenuItem>
        </MenuItem>
        <MenuItem Header="Edit"></MenuItem>
    </Menu>
    <TabControl Grid.Row="1">
        <TabItem Header="Document Flow" >
            This is where the outline of the entire document will be placed.
            <Canvas></Canvas>
         </TabItem>
        <TabItem Header="Preview">
            This is where the preview will be drawn to screen.
        </TabItem>
        <TabItem Header="Resources">
            This is where the resources { graphic files, fonts, data files }
        </TabItem>
        <TabItem Header="Code Library">
            This is where the user can save re-usable bits of code. Useful when adding intel barcodes or Address blocks etc...
        </TabItem>
    </TabControl>


    <StatusBar Grid.Row="2">
        Items
    </StatusBar>
</Grid>

2
TabItem只能包含一个元素。您是否真的尝试将您的内容添加到Canvas标签下或与Canvas处于同一级别(这样无法工作)? - Ferenc Kun
3个回答

62
通过将文本描述添加到您的TabItem中,您添加了Content,然后当您添加Canvas时,您添加了一个额外的Content项,这是不允许的TabItem。 您需要使用可以容纳Children集合的控件,例如Canvas、Grid、StackPanel等。尝试像这样做。
<TabControl Grid.Row="1">
    <TabItem Header="Document Flow">
        <Canvas>
            <TextBlock>
                This is where the outline of the entire document will be placed.
            </TextBlock>
        </Canvas>
    </TabItem>
    <TabItem Header="Preview">
        This is where the preview will be drawn to screen.
    </TabItem>
    <TabItem Header="Resources">
        This is where the resources { graphic files, fonts, data files }
    </TabItem>
    <TabItem Header="Code Library">
        This is where the user can save re-usable bits of code. Useful when adding intel barcodes or Address blocks etc...
    </TabItem>
</TabControl>

2
谢谢。完美地工作了。由于您的解释清晰,我不得不给您接受的答案。 - bernieslearnings

12

某些容器只允许1个元素,其他容器允许>1个元素。 当您收到错误消息“Content”设置多次时,这意味着您尝试将多种类型的元素放入仅允许1个元素的容器中。

也许可以尝试这个(未经测试):

<TabItem Header="Document Flow" >
<StackPanel>
<TextBlock>This is where the outline of the entire document will be placed. </TextBlock>
<Canvas></Canvas>
</StackPanel>
</TabItem>

4
尝试将TabItem的内容包装在Grid中,并使用TextBlock显示文本:
<TabItem Header="Document Flow" >
    <Grid>
        <TextBlock Text="This is where the outline of the entire document will be placed."/>
        <Canvas></Canvas>
    </Grid>
</TabItem>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接