Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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.

like image 712
totoro Avatar asked Oct 23 '14 09:10

totoro


1 Answers

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

like image 135
Charleh Avatar answered Sep 22 '22 00:09

Charleh