开发者

ResourceDictionary in application resources cannot be found

I'm facing some strange issue with a ResourceDictionary in my Application.Resources. In order to fill multiple ComboBoxes inside my application, I want to store the data in a ResourceDictionary.

However, I get the "Cannot find a Resource with that Name/Key..." error constantly.

My XAML-Code here:

<Application.Resources>
    <ResourceDictionary x:Key="RDArray">
        <sys:String x:Key="item1">Item1</sys:String>
        <sys:String x:Key="item2">Item2</sys:String>
        <sys:String x:Key="item3">Item3</sys:String>
    </ResourceDictionary>
</Application.Resources>

<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}" />

Due to some lucky circumstances I was able to find out that putting another resource like Style above the Dictionary solves the problem.

<Application.Resources>
    <Style x:Key="fubar" />
    <ResourceDictionary x:Key="RDArray">
        <sys:String x:Key="item1">Item1</sys:String>
        <sys:String x:Key="item2">Item2</sys:String>
        <sys:String x:Key="item3">Item3</sys:String>
    </ResourceDictionary>
</Application.Resources>

The "bug" occurs in a WPF application as well as in Silverlight.

Althou开发者_JAVA百科gh I can solve this using the shown "trick", I am curious where this error is coming from. I wasn't able to find anything about this. Maybe it is just me and something I am understanding wrong about resources in WPF.


This is because if you have a resource dictionary as the only item in the resources section then the contents simply get added to the parent dictionary (I snooped and this seems to be the case). To get around this you need to put your resource dictionary in separate xaml file (List.xaml in this case):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary x:Key="RDArray">
        <sys:String x:Key="item1">Item1</sys:String>
        <sys:String x:Key="item2">Item2</sys:String>
        <sys:String x:Key="item3">Item3</sys:String>
    </ResourceDictionary>
</ResourceDictionary>

and then reference that in your main app:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="List.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}"/>


Thanks for your answer. In my real application I created that separated file, but I didn't make a second ResourceDictionary inside of it a nd just put the string values there. So it looked like:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <sys:String x:Key="item1">Item1</sys:String>
    <sys:String x:Key="item2">Item2</sys:String>
    <sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>

So this was of course not working either.

Thanks for your answer, definitely solves this one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜