totoro
totoro

Reputation: 3407

WPF Error: Property elements cannot be in the middle of an element's content. They must be before or after the content

I have a MergedDictionaries and DateTemplate inside a ResourceDictionary and everything was fine until I added a Converter:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WPFTry">

    <local:IsEnabledConverter x:Key="isEnabled"/>  <===== causes problem

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <DataTemplate x:Key="fileinfoTemplate" DataType="{x:Type local:MyFileInfo}">
        ... template stuff
    </DataTemplate>

</ResourceDictionary>

Adding the Converter line causes this error at the line of DataTemplate:

Property elements cannot be in the middle of an element's content. They must be before or after the content.

Why is causing this error?

Note that the code compiles and the Converter works fine if I comment out the MergedDictionaries.

Upvotes: 9

Views: 11103

Answers (2)

zar
zar

Reputation: 12247

You can get the same error if the there is incorrect XAML in file which may not be obvious (the error message is not too specfic).

For example the following will produce this error:

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="1"> <!-- delete starting this line to fix -->
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions> 
        </Grid> <!-- delete ending this line to fix -->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
                <Button Grid.Row="0" Content="Button1"/>
                <Button Grid.Row="1" Content="Button2"/>
                <Button Grid.Row="2" Content="Button3"/>
        </Grid>
    </Grid>

The fix is to delete the blocked marked in the XAML above.

Upvotes: 1

Charleh
Charleh

Reputation: 14002

The error tells you the issue:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WPFTry">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles.xaml" />
    </ResourceDictionary.MergedDictionaries>

   <!-- Move this here -->
   <local:IsEnabledConverter x:Key="isEnabled"/>

   <DataTemplate x:Key="fileinfoTemplate" DataType="{x:Type local:MyFileInfo}">
        ... template stuff
    </DataTemplate>

</ResourceDictionary>

You are trying to put content before setting properties on the resource dictionary. The error says "property elements" (e.g. ResourceDictionary.MergedDictionaries) cannot be in the middle of an elements "content" (e.g. your datatemplate/converters etc)

Anything that has a dot . must appear at the top of an element as you are essentially setting properties in the XAML. Anything that doesn't have a . is content and must appear below any property setters.

Note: this also works the other way round, properties can also be below all the content if you prefer it that way round

Upvotes: 13

Related Questions