XAML - The property 'Content' is set more than once

Wpf

Wpf Problem Overview


Very new to WPF and XAML. I can't get my head around why I can't place a WPF control where I would like in the following code. My issue is where the <canvas></canvas> tags are. Anything I put in this place gives me 'The property 'Content' is set more than once'

If anyone could explain in simple terms where the Content property is set that would be most helpful.

I have checked out the following articles to no avail: https://stackoverflow.com/questions/1331456/the-property-content-is-set-more-than-once https://stackoverflow.com/questions/8639738/the-property-content-is-set-more-than-once https://stackoverflow.com/questions/10384395/property-content-is-set-more-than-once https://stackoverflow.com/questions/14478253/the-property-content-is-set-more-than-once-button-wpf https://stackoverflow.com/questions/10706043/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>

Wpf Solutions


Solution 1 - Wpf

By adding your text description to your TabItem you added Content then when you added the Canvas you added an additional item of Content which is not allowed for the TabItem. You need to use a Control that can hold a collection of Children such as Canvas, Grid, StackPanel etc. Try something like this.

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

Solution 2 - Wpf

Certain containers only allow 1 element, other containers allow >1 element. When you get the error message 'Content' is set more than once, it means you have tried to put more than one type of element in a container that only allows 1 element.

Maybe try this (not tested):

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

Solution 3 - Wpf

Try to wrap a content of TabItem in a Grid and use TextBlock to show text:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbernieslearningsView Question on Stackoverflow
Solution 1 - WpfMark HallView Answer on Stackoverflow
Solution 2 - WpffailedprogrammingView Answer on Stackoverflow
Solution 3 - WpfAndrey GordeevView Answer on Stackoverflow