2

I have a UserControl that consists of a listview it looks like:

<UserControl
           ....

    <UserControl.Resources>

        <Style TargetType="Thumb">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="GridViewColumnHeader">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollBar}">
            <!-- Style Content -->
        </Style>

        <Style  TargetType="{x:Type ScrollViewer}">
            <!-- Style Content -->
        </Style>

        <Style TargetType="{x:Type ListViewItem}">
            <!-- Style Content -->
        </Style>

    </UserControl.Resources>

    <ListView Name="ListView1" >
            <!-- ListViewContent -->
    </Style>
</UserControl>

I have 3 of those userControls where the only thing that is different between them is the styles in <UserControl.Resources>. It makes no scene to have to create multiple controls that have the same functionality just because I need a different look and feel. What I want to do now is combine all the styles in <UserControl.Resources> into one style. If I manage to group all those styles into one I would be able to remove the 3 controls and change the style as:

  <ListView Style={DynamicResource style1} ...

Currently if I do

<UserControl.Resources>
     <Style x:Key="style1">
         <!-- Place all styles in here -->
     </Style>
</UserControl.Resources>

It does not work.


Edit

Thanks to iltzortz answer I now have:

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Green"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Red"></SolidColorBrush>

</ResourceDictionary>

Dictionary2.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Grid">        
        <Setter Property="Background" Value="Black"></Setter>
    </Style>

    <SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>

</ResourceDictionary>

MyUserControl:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="97" d:DesignWidth="91">

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

    <Grid >
        <Ellipse Fill="{DynamicResource Foo}" />
    </Grid>
</UserControl>

And I change resource dictionaries dynamically like this: switching wpf resource dictionaries at runtime

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • i also found that you can use something like this code: `this.targetUserControl.Resources.Source = new Uri("common2.xaml",UriKind.Relative);` to change it – iltzortz Jan 25 '13 at 19:29

2 Answers2

1

Add a resource dictionary to your application named e.g. common.xaml

enter image description here

and put your common styles there

then you can reuse it with:

 <UserControl.Resources>
    <ResourceDictionary Source="common.xaml"/>
 </UserControl.Resources>
iltzortz
  • 2,342
  • 2
  • 19
  • 35
  • +1 iltzortz thanks for the help! it works but I don't know how to change the resource dynamically. Take a look at my edit that I am working on now. – Tono Nam Jan 25 '13 at 19:10
0

You can create 3 resource dictionaries and merge them at runtime. In my example code I used two resource dictionaries.

Example:

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="0,10,0,0" />
    </Style>
</ResourceDictionary>

Dictionary2.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="btnStyle" TargetType="Button">
        <Setter Property="Margin" Value="50,50,0,0" />
    </Style>

</ResourceDictionary>

In the start of application you can set default style in App.xaml file:

<Application x:Class="Example.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

If you want to change style, you can merge resource dictionaries:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("\\Dictionary2.xaml", UriKind.Relative);            
this.Resources.MergedDictionaries.Add(dict);

And now binding looks like this:

<Button Style="{DynamicResource btnStyle}" Content="Click me!" />

Now if you invoke code to merge resource dictionaries, button style will be automatically changed.

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65