In this tip, I’d like to introduce you to the concept of XAML tree walking. This is mostly used for searching referenced resources or searching the datacontext in a databinding scenario. By automatically searching/walking the XAML tree for us, Silverlight lets us define resources in a shared location so we can re-use them throughout the application.
The code for this tip can be found here.
Defining a resource
While we can define several types of objects (in fact every class can be instantiated in resources) in a resource block, most often, we’ll use styles. We’ll use that in this sample as well. In the following code, I have defined a style and placed it in the application resources.
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="XamlTreeWalking.App"
>
<Application.Resources>
<Style x:Key="TitleTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontFamily" Value="Courier New" ></Setter>
</Style>
</Application.Resources>
</Application>
Using the resource
Now that we have the resource defined, we can use it as follows:
<Grid x:Name="LayoutRoot" Background="LightBlue">
<StackPanel>
<Grid>
<StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockStyle}"
Text="Welcome to SilverlightShow.net"></TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
Nowhere are we indicating Silverlight that it should look in the App.xaml for this resource. Instead, it searches and finds it automatically. This is done through XAML tree walking: Silverlight searches up the XAML tree to see if it can locate the referenced resource. If not, it searches up a level and so on, as shown in the image below.
If the resource can’t be found on the UserControl level, Silverlight will look at the highest level, which is the App.xaml.
If we run the sample, we see the following:
What now if Silverlight would encounter a resource with the same name at a “lower level”, meaning earlier in the search process? Assume that we have a resource with the name TitleTextBlockStyle defined also on the LayoutRoot Grid like so:
<Grid x:Name="LayoutRoot" Background="LightBlue">
<Grid.Resources>
<Style x:Key="TitleTextBlockStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="30"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontFamily" Value="Comic Sans MS" ></Setter>
</Style>
</Grid.Resources>
<StackPanel>
<Grid>
<StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockStyle}"
Text="Welcome to SilverlightShow.net"></TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
Silverlight will stop searching when it first encounters a match, thus resulting in the following being shown when the application is ran:
We can clearly see the Comic Sans being used here. Note also that there’s no cascading being applied here: Silverlight is just using the first encountered style, not a combination of the 2!
Summary
In this tip, we looked at the concept of XAML tree walking to locate a referenced resource in a Silverlight application. We also learned that there’s no cascading being applied here.
About Gill
Gill Cleeren is Microsoft Regional Director (www.theregion.com), Silverlight MVP (former ASP.NET MVP), INETA speaker bureau member and Silverlight Insider. He lives in Belgium where he works as .NET architect at Ordina. Passionate about .NET, he’s always playing with the newest bits. In his role as Regional Director, Gill has given many sessions, webcasts and trainings on new as well as existing technologies, such as Silverlight, ASP.NET and WPF at conferences including TechEd Berlin 2010, TechDays Belgium, DevDays NL, NDC Oslo Norway, SQL Server Saturday Switserland, Spring Conference UK, Silverlight Roadshow in Sweden… He’s also the author of many articles in various developer magazines and for SilverlightShow.net. He organizes the yearly Community Day event in Belgium.
He also leads Visug (www.visug.be), the largest .NET user group in Belgium. Gill recently published his first book: “Silverlight 4 Data and Services Cookbook” (Packt Publishing). You can find his blog at www.snowball.be.
Twitter: @gillcleeren