Daryl Gill
Daryl Gill

Reputation: 5524

Content is set more than once

Whilst playing about with WPF in Visual Studio 2013, I have been presented with an error:

Error 2 The property 'Content' is set more than once.

Error 1 The property "Content" can only be set once

Now, first of all. I turn to google for the error message & got presented with a top results linking to StackOverflow.

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

the property 'Content' is set more than once

Property content is set more than once

Including a MSDN post:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/686be076-954f-4373-a2e6-fc42a6a27718/error-the-property-content-is-set-more-than-once?forum=wpf

Whilst being presented with an informative collection of tailored solutions based on the original posters code, I have yet to come across an actual basic solution detailing the reasons of this error (XAML Novice) Whilst this may be a duplicate of multiple reported problems. I personally would rather avoid posting a problematic code to get a tailored solution. I'd much rather come here and ask the community reasons as to why a novice XAMP/WPF Developer might encounter this application and solutions & not so much top, top best practices. Rather words of advice from the more WPF/XAMP Developers on how to easily identify the solution and continue on with furter debugging steps in the future


For argument sake:

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

Upvotes: 4

Views: 17541

Answers (2)

Northik
Northik

Reputation: 342

A window can only contains 1 element.

In your code

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <TextBlock>Hello</TextBlock>
    <TextBlock>World</TextBlock>
</Window>

your window have 2 textblock u shoudl try something like

<Window x:Class="WPFT.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="197.198" Width="427.95">
    <Grid>
        <TextBlock>Hello</TextBlock>
        <TextBlock>World</TextBlock>
    </Grid>
</Window>

Upvotes: 10

Jaime Still
Jaime Still

Reputation: 1998

If you are setting more than one element inside of any UIElement with the Content dependency property, you will get this error. You need to wrap multiple elements inside of a panel so that the Content property only has one child element. For instance...

<Button>
    <StackPanel Orientation="Horizontal">
        <Image />
        <TextBlock />
    </StackPanel>
</Button>

<Border>
    <StackPanel>
        <TextBlock />
        <Image />
        <DatePicker />
    </StackPanel>
</Border>

The areas between the Button and Border elements are shorthand for specifying:

<Button>
    <Button.Content>
        <!-- Content goes here -->
    </Button.Content>
</Button>

Upvotes: 6

Related Questions