CoreCLR's Blog

Archive for the ‘Blend’ Category

Multiple ResourceDictionary’s with Blend Support

Posted by coreclr on October 29, 2009

On any real WPF/Silverlight project, you end up with a lot of Styles/Templates. The recommended approach is to put these in a separate ResourceDictonary.

But pretty soon, this ResourceDictonary can become very large, so we need to split it into multiple ResourceDictonary’s, but at the same time we don’t want to loose Blend Design Support.

Lets say we have a Style for ComboBox and a Style for a Button. Both styles are Based on another Style called BaseStyle. Here is a snippet of the Styles:

<Style x:Name="BaseStyle" TargetType="Control">
           <Setter Property="FontSize" Value="20" />

       <Style x:Key="ComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource BaseStyle}">
           <Setter Property="Padding" Value="6,2,25,2"/>
           <Setter Property="Background" Value="#FF1F3B53"/>
           <Setter Property="HorizontalContentAlignment" Value="Left"/>
           <Setter Property="BorderThickness" Value="1"/>
           <Setter Property="TabNavigation" Value="Once"/>

       <Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
           <Setter Property="Background" Value="#FF1F3B53"/>
           <Setter Property="Foreground" Value="#FF000000"/>
           <Setter Property="Padding" Value="3"/>
           <Setter Property="BorderThickness" Value="1"/>

As long as these Styles exist in the same ResourceDictonary, then everything is good.

Now lets say we want to put the Button Style in a ResourceDictonary called ButtonResources.xaml and the ComboBoxStyle in a ResourceDictonary called ComboBoxResources.xaml.

But what do we do about the BaseStyle? We don’t want to duplicate it in both files.

One way is to create a new ResourceDictonary (BaseResources.xaml) and place it in that ResourceDictonary.

But now we lost Design-time support in Blend…

The trick here is pretty simple: just include BaseResources.xaml in both ButtonResources.xaml and ComboBoxResources.xaml:

Put this section just before the Styles in both files:

           <ResourceDictionary Source="BaseResources.xaml" />

(Please note that you cannot have circular reference).

In our Main app we can just include the two files and everything runs:

         <ResourceDictionary Source="/SilverlightThemes;component/Themes/ButtonResources.xaml" />
         <ResourceDictionary Source="/SilverlightThemes;component/Themes/ComboBoxResources.xaml" />

Posted in .NET, Blend, Silverlight, WPF | Leave a Comment »

Expression Blend for Developers Video

Posted by coreclr on September 25, 2009

Andrew Troelsen did a very nice video about blend for developers. Find it here:

Posted in Blend, Video | Leave a Comment »