Question

i'm using a listbox style twice in my project so i want to use it in a resource dictionary. This listbox style has two value converter's in it, so i instantiated the converters in the same resource file. at runtime though it says that the 'unknown type cannot be declared' although the same converter declarations work when using them in the mainwindow.xaml file.

Anyone have an idea?

Was it helpful?

Solution

I had the same issue until I moved the converter into the App.xaml file under the resources section. This is my sample App.xaml file, I just created a converter named TextConverter for the sample.

There are two ways to do this if you are using other ResourceDictionaries you have to use the ResourceDictionary.MergedDictionaries in all cases as seen below:

<Application x:Class="WPFFeatureSample_Application.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter>
            </ResourceDictionary>
            <ResourceDictionary Source="Resources/ControlDictionary.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

If you do not have other resource files it would look like this:

<Application x:Class="WPFFeatureSample_Application.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:converters="clr-namespace:WPFFeatureSample_Application.Converters"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <converters:TextConverter x:Key="TextConverter1"></converters:TextConverter>
    </ResourceDictionary>
</Application.Resources>

One interesting reference when using converters and Resource Dictionaries in general is this: http://www.dotnetdude.com/2011/01/23/ResourceDictionariesAreMakingMyHairHurt.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top