微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF错误:属性元素不能位于元素内容的中间.它们必须在内容之前或之后

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

添加Converter行会导致DataTemplate行出现此错误

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

为什么会导致此错误

请注意,如果我注释掉MergedDictionaries,代码将编译并且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">

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

您正在尝试在资源字典上设置属性之前放置内容.错误说“属性元素”(例如ResourceDictionary.MergedDictionaries)不能位于元素“content”的中间(例如你的datatemplate / converter等)

任何有点的东西.必须出现在元素的顶部,因为您实际上是在XAML中设置属性.任何没有的东西.是内容,必须出现在任何属性设置者下面.

注意:这也适用于其他方式,如果您喜欢这种方式,属性也可以低于所有内容

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐