烦人的XAML问题:我在资源字典文件中定义了一个DataTemplate,该文件必须访问在App.Resources中定义为资源的转换器。从逻辑上讲,我应该将DataTemplate字典与App.Resources字典合并,应该这样。但是我有一个例外,说找不到我的转换器资源。我想念什么吗?参考?定义顺序?
更新:这是我的App.Resources
<ResourceDictionary>
<!--Global Resources-->
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
<!--System Resources-->
<sys:Boolean x:Key="True">True</sys:Boolean>
<sys:Boolean x:Key="False">False</sys:Boolean>
<!--Framework Resources-->
<fr:EnumToBoolConverter x:Key="EnumToBool"/>
<fr:EnumAttributeConverter x:Key="EnumToAttr"/>
<fr:FileInfoConverter x:Key="ToFileInfo"/>
<fr:ImageInfoConverter x:Key="ToImageInfo"/>
<fr:UnitConverter x:Key="ToUnit"/>
<fr:CommandParameterConverter x:Key="ToCmdParam"/>
<!--Style Resources-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/FrameworkUI;component/Styles/Dark3DStyles.xaml"/>
<ResourceDictionary Source="/Resources/DataTemplates.xaml/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...这是在资源字典文件中定义的DataTemplate
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="NoteEnumTemplate">
<Grid Height="22">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Stretch="None"
Source="{Binding Converter={StaticResource EnumToAttr},
ConverterParameter=ICON}">
<Image.Effect>
<DropShadowEffect ShadowDepth="0"/>
</Image.Effect>
</Image>
<Label Grid.Column="1"
Content="{Binding Converter={StaticResource EnumToAttr},
ConverterParameter=DESCR}"/>
</Grid>
</DataTemplate>
找不到的转换器是EnumToAttr
您的问题是定义资源/数据模板的顺序以及合并字典的顺序。
您正在将ResourceDictionary合并到App.Resources。因此ResourceDictionary是最重要的,并且必须具有每个要运行的资源(键)。它不能使用在派生的ResourceDictionary中定义的键。
将转换器移到ResourceDictionary中,然后查看结果。
您已在最后编写了合并字典代码,但不会这样做。如果您看到编译后将生成的代码,则资源必须在上述顺序中。
您的设计前景解决方案:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="RD2.xaml" />//Converter code
<ResourceDictionary Source="RD.xaml" /> //Template code
</ResourceDictionary.MergedDictionaries>
上面的代码将起作用,因为始终会在模板代码之前生成转换器代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句