Geysser Sdz
Geysser Sdz

Reputation: 85

XAML DataTemplate can't access App resource

Annoying XAML problem : I define a DataTemplate in a resource dictionary file, that has to access a converter defined as a resource in App.Resources. Logically, I should merge my DataTemplate dictionary with the App.Resources dictionary and that should be it. But I get an exception saying that my converter resource can't be found. Am I missing something? A reference? Order of definition?

Update: Here is my 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>

...and here is the DataTemplate defined in a resource dictionary file

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

The converter that can't be found is the EnumToAttr

Upvotes: 3

Views: 865

Answers (1)

Kylo Ren
Kylo Ren

Reputation: 8803

Your problem is the order in which you have defined the resources/Data Template and the order in which you have merged the dictionaries.

You are merging ResourceDictionary to App.Resources. So ResourceDictionary is top most and must have every resource(key) to run. It can't use a key that is defined in a derived ResourceDictionary .

Move the Converter in ResourceDictionary and then see the result.

you have written the merge dictionary code at the last but that will not do. The resource have to be in above order if you see in prospect of the code that will get generated after compilation.

Solution from your design prospect:

<ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="RD2.xaml"  />//Converter code
            <ResourceDictionary Source="RD.xaml"  /> //Template code            
</ResourceDictionary.MergedDictionaries>

Above code will work cause converter code always will be generated before Template code.

Upvotes: 4

Related Questions