Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of StaticResource within a WPF ResourceDictionary?

I have a WPF ResourceDictionary with the following TextBlock:

<TextBlock Visibility="{Binding Converter={StaticResource MyBoolProp ResourceKey=BoolToVis}}">
</TextBlock>

The ResourceDictionary is included in App.xaml under MergedDictionaries:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyResourceDictionary.xaml"/>

Within the App.xaml I have defined the BoolToVis converter (again, under Application.Resources)

<BooleanToVisibilityConverter x:Key="BoolToVis" />

When I start my app up - I get the following XamlParseException:

"Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception."

The InnerException is:

"Cannot find resource named 'BoolToVis'. Resource names are case sensitive."

I'm able to refer to this converter directly with App.xaml (in fact, the particular XAML declaration is identical) and within other UserControls with no problems.

This particular bit of code also worked fine under the .NET 4.0 RC (and Beta2). This error only started happening when I upgraded to the .NET 4.0 RTM.

I'm able to work around it by declaring another BooleanToVisibilityConverter within MyResourceDictionary.xaml and referring to it like so:

<TextBlock Visibility="{Binding Converter={StaticResource MyBoolProp ResourceKey=BoolToVis2}}">
</TextBlock>

Any reason why I should need to do this?

like image 521
Nicolas Webb Avatar asked Apr 20 '10 21:04

Nicolas Webb


People also ask

What is static resource in WPF?

Static Resource - Static resources are the resources which you cannot manipulate at runtime. The static resources are evaluated only once by the element which refers them during the loading of XAML.

What is static resources in C#?

Static ResourceIt will not change once it's assigned and they are applied at the compiled time only. Add 2 static resources, one for button's background & another for the button's border. <Window x:Class="A.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

What are resources in WPF?

A resource is an object that can be reused in different places in your application. WPF supports different types of resources. These resources are primarily two types of resources: XAML resources and resource data files. Examples of XAML resources include brushes and styles.


1 Answers

Per MSDN:

Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into.

Resources defined in App.xaml cannot be seen by a merged ResourceDictionary. I would think it makes more sense to define a converter used in a ResourceDictionary in the ResourceDictionary itself, or another ResourceDictionary which houses all your converters.

like image 143
David Avatar answered Oct 04 '22 16:10

David