我有一个MergedDictionaries和DateTemplate里面的ResourceDictionary一切,一切都很好,直到我添加一个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>
Run Code Online (Sandbox Code Playgroud)
添加该Converter行会导致此错误DataTemplate:
Property elements cannot be in the middle of an element's content. They must be before or after the content.
Run Code Online (Sandbox Code Playgroud)
为什么会导致此错误?
请注意,如果我注释掉,代码编译并且转换器工作正常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>
Run Code Online (Sandbox Code Playgroud)
您正在尝试在资源字典上设置属性之前放置内容.错误说"属性元素"(例如ResourceDictionary.MergedDictionaries)不能位于元素"内容"的中间(例如,您的数据模板/转换器等)
任何带点的东西都.必须出现在元素的顶部,因为您实际上是在XAML中设置属性.任何没有.内容的东西都必须出现在任何属性设置者的下方.
注意:这也适用于其他方式,如果您喜欢这种方式,属性也可以低于所有内容