March 31, 2011

Using Xaml resources in a common assembly

First put the resources in a resource dictioanry xaml file that goes into the shared resource assembly. In this sample the assembly is called Common and the common resources are placed in a subdirectory WPF of this assembly. Their is no ".cs" file associated with this Xaml file. Here is the Xaml file in the "Common" assembly with a shared listbox style called "MyListStyle":
            <Style x:Key="MyListStyle" TargetType="ListBox">

To reference these resources in another WPF component, do it from the Resources area of the WPF component. For example
<Window ... >
         <ResourceDictionary Source="/Common;component/WPF/Resources.xaml" />
        <ListBox ... Style="{StaticResource MyListStyle}" />

To reference those resources from a WPF Application:
<Application ... >
                <ResourceDictionary Source="/Common;component/WPF/Resources.xaml"/>

Once added to an application's resources they can be accessed anywhere in the normal fashion:
<Window ... >
        <ListBox ... Style="{StaticResource MyListStyle}"  />

No comments: