4

Inside a XAML Page I'm trying to use an IValueConverter, it's throwing an error.

  • The IValueConverter is in another assembly, I have added a reference
  • There are no design-time errors
  • I have assigned the StaticResource with a ResourceKey

At the top of my page I have this:

xmlns:converters="clr-namespace:Converters;assembly=Converters"

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/DialogStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <converters:NoWhiteSpaceConverter x:Key="NoWhiteSpaceConverter" />
    </ResourceDictionary>
</Page.Resources>

Then I try to use it later on like this:

<TextBox Text="{Binding SomeText, Converter={StaticResource NoWhiteSpaceConverter}}" />

Can anyone see what the problem is?

Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • @Sebastian, The error is the title of the question. – Drahcir Dec 13 '12 at 14:30
  • 1
    Can you put the resource into the App.xaml? Maybe you ran into a problem like this one: http://www.paulkiddie.com/2011/10/the-importance-of-the-position-of-window-resources-element-in-wpf-xaml-markup/ – Sebastian Dec 13 '12 at 14:36
  • I'm having the same issue, reloading the designer seems to solve the issue untill you restart visual studio... – Dtex Dec 13 '12 at 14:39
  • @DTex, No, not that this time, although that did fix something else yesterday. – Drahcir Dec 13 '12 at 14:42
  • @Sebastian, Thanks, this is working now. If you post as answer and I will mark. – Drahcir Dec 13 '12 at 14:42
  • @Sebastian Yes sorry i got it the other way around, i have a design time error but no error at runtime – Dtex Dec 13 '12 at 14:51
  • Forgot to call `InitializeComponent()` somewhere? – Harry Jan 17 '17 at 09:15

2 Answers2

17

Make sure that the resources are defined before the usage (in Xaml parsing order). The easiest way is to place it into App.xaml

See also here for a similar issue: https://paulkiddie.com/the-importance-of-the-position-of-window-resources-element-in-wpf-xaml-markup/

Sebastian
  • 7,729
  • 2
  • 37
  • 69
1

In my case the resource was correctly defined before it is used but there was a wrong reference to a converter.

The problematic line was

...{Binding MyProperty, Converter={StaticResource local:MyConverter}}

and it should be without the namespace alias

...{Binding MyProperty, Converter={StaticResource MyConverter}}

where local is a namespace alias containig the converter class and MyConverter is the key defined in a resource in the XAML.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16