Drahcir
Drahcir

Reputation: 11972

Provide value on 'System.Windows.StaticResourceExtension

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

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?

Upvotes: 5

Views: 20942

Answers (2)

Sebastian
Sebastian

Reputation: 8005

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/

Upvotes: 18

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.

Upvotes: 1

Related Questions