Why the StaticResource cannot be resolved in this case?
Asked Answered
C

3

16

I have got an exception "Cannot find resource named 'mrg'. Resource names are case sensitive." when I try to do the following:

MainWindow.xaml:

<Window.Resources>
  <Thickness Left="0"
             Right="1"
             Bottom="2"
             Top="3"
             x:Key="mrg" />
</Window.Resources>
<Grid>
  <ItemsControl ItemsSource="{Binding}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <local:UserControl1 />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</Grid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<string> source = new List<string>()
        {
            "item1",
            "item2",
            "item3",
            "item4",
            "item5",
        };
        DataContext = source;
    }
}

and UserControl1.xaml:

<Grid>
    <TextBlock Text="{Binding}" Margin="{StaticResource mrg}" />
</Grid>

According to the msdn article:

Static resource lookup behavior

  1. The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.

  2. The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.

  3. Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.

So the resource had to be found because of step 2. But, as I can see in the Locals window when exception is catched, the UserControl1.Parent == null.

I'm confused in this problem. The way I can solve it is to put the resource to the Application level.

My question is: why the StaticResource connot be found ?

Cnemis answered 22/1, 2013 at 8:52 Comment(0)
T
17

The DataTemplate forms a logical tree of its own, which is disconnected from the logical tree of the ItemsControl. Hence the lookup by traversing the logical tree won't find the resource.

I wasn't able to find a reference in MSDN, just this article on CodeProject, where it reads:

The elements that are part of an expanded template, hereafter referred to as "template elements", form their own logical tree which is disconnected from the logical tree of the object for which they were created.


Using DynamicResource instead of StaticResource will overcome the problem. However i can't tell exactly why. Maybe an explanation can be found in the Static resource lookup behavior and Dynamic resource lookup behavior sections in Static and Dynamic Resources, but i'm not sure.

Tweeter answered 22/1, 2013 at 10:15 Comment(5)
Good suggestion, I will continue investigating this issue.Cnemis
In my case I fixed it by merging in the resource dictionary inside the window or usercontrol that needed it, instead of merging it in "globally" at App.xaml. All looked good at design time, but got this exception at runtime, so it was a bit puzzling.Amsterdam
@SoMoS were you using some IoC? I had the same problem and changing to Dynamic solved as well. I'm using MEF to load views from TViewModel by name convention.Douville
In this particular case I was not using it :\Zacynthus
DynamicResource did work when I tried it but given that it appears to tie things in at runtime, I was apprehensive about using it. In my case it turned out I had a made a rookie mistake in my declaration order, which I fixed after reading the answer here: how-to-reference-a-static-resource-from-within-a-datatemplateTojo
U
3

I had a similar problem after a code cleanup. Code looked and compiled fine but would produce a "StaticResource not found error" at run time.

Changing StaticResource to DynamicResource did work. But, it really came down to App.xaml and the order in which resource dictionaries were added under the <ResourceDictionary.MergedDictionaries> section.

Undermanned answered 1/2, 2016 at 15:29 Comment(0)
B
2

StaticResources must be defined before referring to them.

Faced with the unknown static link to the resource, the XAML parser throws an exception.(This problem can be solved by using a dynamic resource, but it carries the additional costs)

Breunig answered 17/8, 2016 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.