Home > View Post

Xaml. Resource Dictionaries

Now that we've found out a little about resources and how useful they can be for storing things (and more importantly, reducing repetition in our Xaml) lets look at how we can reuse them across multiple Xaml files.

Enter the ResourceDictionary:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    
    <sys:DateTime x:Key="date1">20070711</sys:DateTime>
    <sys:DateTime x:Key="date2">20071225</sys:DateTime>
    
</ResourceDictionary>

Wow! Now we can easily reuse our two dates (!? I know, I know) by referencing our resource dictionary in any other Xaml file:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

Note that the dictionaries are merged. So if you have two things in with the same key, the last one wins.

Tags: Xaml

 
Josh Post By Josh Twist
12:01 AM
28 Feb 2007

» Next Post: Xaml. Embedding code
« Previous Post: Xaml. Using Resources

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.