Scott Perham
Scott Perham

Reputation: 2470

WPF cannot find static resource

I have an element defined in a resource dictionary like so:

<SolidColorBrush x:Key="ChromeBrush" Color="Red"  />

I'm trying to use this as the background of a Border as shown below:

<Style TargetType="ToolBar">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolBar">
                <Border x:Name="Border" Background="{StaticResource ChromeBrush}">
                    <DockPanel>
                        <ToolBarPanel x:Name="PART_ToolBarPanel" IsItemsHost="true" Margin="0,1,2,2" />
                    </DockPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

For this specific usage I get an error saying Cannot find resource named 'ChromeBrush'. Resource names are case sensitive.

If I change the static resource usage to just Red all works as expected.

The User Control that is hosting the Toolbar is being dynamically created at runtime (via Activator.CreateInstance(...)) - In case it helps, the XAML for the User Control is:

<framework:PanelBase x:Class="Framework.UserControl1"
            namespaces omitted for brevity
            >

    <framework:PanelBase.ToolbarTemplate>
        <ToolBar />
    </framework:PanelBase.ToolbarTemplate>

    <!-- other controls -->
</framework:PanelBase>

The bizarre thing is that the brush is used elsewhere in the project a number of times in various controls in exactly the same way with no issues (some usages are actually on the hosting User Control). In the snippet above, Visual Studio underlines the resource in the correct colour so it is able to resolve it.

UPDATE: After wondering if it's something to do with the fact that the UserControl1 class is in a different (dynamically loaded) assembly to the resource dictionary, I tried using the resource on another control and it works fine... something specifically to do with that Toolbar style.

<framework:PanelBase x:Class="Framework.UserControl1"
            namespaces omitted for brevity
            >

    <framework:PanelBase.ToolbarTemplate>
        <ToolBar />
    </framework:PanelBase.ToolbarTemplate>

    <Border Background="{StaticResource ChromeBrush}">
        <!-- WORKS FINE! -->
    </Border>
</framework:PanelBase>

Does anyone have any idea why this might be happening at runtime for this specific usage? Is the fact that it's dynamically created got anything to do with it?

Thanks in advance, Scott

Upvotes: 1

Views: 2486

Answers (1)

Dave M
Dave M

Reputation: 3033

My guess for the reason that it doesn’t work is some quirk of how and when WPF looks up and binds static resources. My guess is that it has more do with how you load the assembly than how you instantiate the object (via reflection with Activator.CreateInstance), but I could be totally off, someone more familiar with WPF internals would have to say for sure.

However, the workaround should be simple: use DynamicResource instead. DynamicResource works more like normal bindings and should have no trouble resolving the references.

Upvotes: 1

Related Questions