Contents tagged with Resources

  • Silverlight 3,0 Split styles and templates into different files and merge resources

    Silverlight 3.0 now support a way to merge resources. So now we can split resources into different files. The following is a example of a simple resource file (ResourceA.xaml):


    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Style x:Name="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="red"/>
        </Style>
    
    </ResourceDictionary>


    When you create a resource file, make sure the Build Action is set to Resource for the file.

    If you want to globally want to merge different resource file you can for example do it on a application level by using the App.xaml file. To merge a Resource file, you need to use the ResourceDictionary object and it’s MergedDictionaries property, which takes ResourceDictionary objects as values. By using the ResourceDictionary’s Source property, you can specify the source for a resource file:


    <Application.Resources>
    
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceA.xaml"/>
                <ResourceDictionary Source="ResourceB.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    
    </Application.Resources>


    You can of course use a resource file on a User Control level:


    <UserControl x:Class="SilverlightApplication6.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:SilverlightApplication6.MyControl"
        Width="400" Height="300">
        <UserControl.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="ResourceA.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </UserControl.Resources>
        
        <Grid x:Name="LayoutRoot" Background="White">
    
            <StackPanel>
                <Button Style="{StaticResource MyButtonStyle}"/>
                <Button Style="{StaticResource MyButtonStyleB}"/>
            </StackPanel>
    
        </Grid>
    </UserControl>


    If you only need to “include” on resource file, you don’t need to use the MergedDictionaries property. Here is an example where only one resource file is used:


    <UserControl.Resources>
        <ResourceDictionary Source="ResourceA.xaml"/>
    </UserControl.Resources>


    A resource file can in turn merge other resource files:


    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceB.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        
        <Style x:Name="MyButtonStyle" TargetType="Button">
            <Setter Property="Background" Value="red"/>
        </Style>
    
    </ResourceDictionary>


    Now I only want to see a way to skip adding the Style attribute to a control, and instead only use the TargetType attribute on a style, and all types should use that style:


    <Style TargetType="Button">
        <Setter Property="Background" Value="red"/>
    </Style>

    Wouldn’t that be something?

    Read more...