C# WPF错误:属性元素不能位于元素A和X27中;它的内容。它们必须在内容之前或之后

C# WPF错误:属性元素不能位于元素A和X27中;它的内容。它们必须在内容之前或之后,c#,wpf,C#,Wpf,我在ResourceDictionary中有一个MergedDictionaries和DateTemplate,在添加转换器之前,一切都很好: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我在
ResourceDictionary
中有一个
MergedDictionaries
DateTemplate
,在添加
转换器之前,一切都很好:

<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>
为什么会导致这个错误


请注意,如果我注释掉
MergedDictionaries

则代码可以编译,转换器工作正常。错误告诉您问题:

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

... 模板材料
您正在尝试在设置资源字典的属性之前放置内容。该错误表示“属性元素”(例如,代码>资源库。合并字典< /COD>)不能处于元素“内容”(例如,数据板/转换器等)的

中。 任何带有点的内容都必须出现在元素的顶部,因为您实际上是在XAML中设置属性。任何没有
的内容都是内容,必须显示在任何属性设置器下面


注意:这也适用于另一种情况,如果您愿意,属性也可以位于所有内容的下方

如果文件中存在不正确的XAML,则可能会出现相同的错误,而这些错误可能并不明显(错误消息不太具体)

例如,以下情况将产生此错误:

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


修复方法是删除上面XAML中标记的阻塞项。

为什么它必须位于顶部?我从未找到“为什么”的答案。为什么它在底部也能工作?这只是一种设计选择。XAML被解析成一个数据结构,我猜每个元素在表示它的结果对象上设置一个属性。也可能是资源字典必须在构建内容之前指定,因为它们可以在XAML中使用,我认为解析器可能足够聪明,可以优先选择某些内容。
“还可能是资源字典必须在构建内容之前指定,因为它们可以在XAML中使用,“
我不认为这是100%正确的。当您将它设置在底部时,您仍然可以在顶部使用它,假设它自上而下构造XAML。@听我说我不是说它们需要在XAML中的内容之上,只是在构建内容之前需要对它们进行解析/准备好使用——这就是我所说的解析器的意思,它足够聪明,可以优先处理。阅读这里:这似乎表明资源词典被视为一个特例。还显示了XAML如何构造和设置属性。是的,任何带有点的内容都必须位于XAML中所有内容的顶部或底部。只是确认一下,除了设计选择和它如何在后台构建XAML之外,没有其他原因?
<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>