XAML - 属性“Content”被设置多次

问题描述 投票:0回答:3

对 WPF 和 XAML 非常陌生。我无法理解为什么我不能在以下代码中将 WPF 控件放置在我想要的位置。我的问题是

<canvas></canvas>
标签在哪里。我放在这个地方的任何东西都会让我“属性‘内容’被设置多次”

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

我查了以下文章没有效果: 属性“Content”被设置多次 属性内容被设置多次 属性内容设置多次 多次设置属性“Content”按钮 WPF ControlTemplate 导致错误“属性‘内容’被设置多次”

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

wpf
3个回答
62
投票

通过将文本描述添加到

TabItem
,您添加了内容,然后当您添加画布时,您添加了
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>

12
投票

某些容器仅允许 1 个元素,其他容器允许 >1 个元素。 当您收到错误消息“内容”被设置多次时,这意味着您尝试将多种类型的元素放入仅允许 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>
© www.soinside.com 2019 - 2024. All rights reserved.