StaticResource extension and lookup behavior

Published Fri, May 7 2010 13:10

While I was investigating Silverlight for my next PT book, I’ve found this official document from MS that describes the lookup algorithm for the StaticResource extension. According to it, here’s the algorithm used by this extension:

The lookup behavior for a StaticResource is that the markup compile pass for XAML first checks whether the object where the actual usage is applied can hold aFrameworkElement.Resources value. If it can, that ResourceDictionary is checked for an item that has that key. This level of lookup is rarely relevant because you usually do not define and then reference a resource on the same object.

I know that English is not my main language, but after reading, and re-reading it I’m really under the impression that it’s saying that StaticResource will always search the element’s resources where it is applied before going through the ancestors in the logical tree. Now, that means that this should work:

<StackPanel x:Name="panel" Orientation="Vertical" Background="{StaticResource brush}">
    <StackPanel.Resources>
        <SolidColorBrush x:Key="brush" Color="Red" />
    </StackPanel.Resources>
    <Button Content="Hi"
            Background="{StaticResource brush}"/>
</StackPanel>

If you look at it, you’ll notice that the StackPanel is trying to use a resource defined in its Resources collection. Unfortunately, it really doesn’t work. Not even in WPF! From a parsing point of view, that’s not that unexpected because the parser hasn’t yet reached the resource definitions when it encounters the StaticResource (I believe that is why you can only reference previously declared resources when creating a new resource).

If I’m not wrong in my analysis, can someone at MS (please!) update the docs?

Filed under:

Comments

# LA.NET [EN] said on Friday, July 09, 2010 8:06 AM

Ok, now that the PT Silverlight book is mostly done (I’m awaiting for feedback form my reviewers – if

# ASPInsiders said on Friday, July 09, 2010 8:22 AM

Ok, now that the PT Silverlight book is mostly done (I’m awaiting for feedback form my reviewers – if

# LA.NET [EN] said on Wednesday, July 14, 2010 1:50 PM

Since I’ve started working in my Silverlight book , I’ve been really busy. So busy that I completely

# ASPInsiders said on Wednesday, July 14, 2010 2:16 PM

Since I’ve started working in my Silverlight book , I’ve been really busy. So busy that I completely

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above:  

Search

This Blog

Tags

Community

Archives

Syndication

Email Notifications

News




  • View Luis Abreu's profile on LinkedIn


    Follow me at Twitter

    My books

    Silverlight 4.0: Curso Completo

    ASP.NET 4.0: Curso Completo

    Portuguese LINQ book cover

    Portuguese ASP.NET 3.5 book cover

    Portuguese ASP.NET AJAX book cover

    Portuguese ASP.NET AJAX book cover