XAML - 属性“内容”设置不止一次

时间:2013-02-14 06:03:34

标签: wpf

WPF和XAML非常新。我无法理解为什么我不能在下面的代码中将WPF控件放在我想要的位置。我的问题是<canvas></canvas>标签的位置。我放在这个地方的任何东西都给了我'属性'内容'设置不止一次'

如果有人能够用简单的术语解释设置内容属性最有帮助的地方。

我检查了以下文章但无济于事: 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>

3 个答案:

答案 0 :(得分:37)

通过将您的文字说明添加到添加了内容的TabItem,然后在添加“画布”时添加了TabItem不允许的其他内容项。你需要使用一个可以容纳collection of 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>

答案 1 :(得分:7)

某些容器仅允许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>

答案 2 :(得分:2)

尝试在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>