Note that there are some explanatory texts on larger screens.

plurals
  1. POwpf merge multiple styles into one style
    primarykey
    data
    text
    <p>I have a UserControl that consists of a listview it looks like:</p> <pre><code>&lt;UserControl .... &lt;UserControl.Resources&gt; &lt;Style TargetType="Thumb"&gt; &lt;!-- Style Content --&gt; &lt;/Style&gt; &lt;Style TargetType="GridViewColumnHeader"&gt; &lt;!-- Style Content --&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type ScrollBar}"&gt; &lt;!-- Style Content --&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type ScrollViewer}"&gt; &lt;!-- Style Content --&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type ListViewItem}"&gt; &lt;!-- Style Content --&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;ListView Name="ListView1" &gt; &lt;!-- ListViewContent --&gt; &lt;/Style&gt; &lt;/UserControl&gt; </code></pre> <p>I have 3 of those userControls where the only thing that is different between them is the styles in <code>&lt;UserControl.Resources&gt;</code>. 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 <code>&lt;UserControl.Resources&gt;</code> 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:</p> <pre><code> &lt;ListView Style={DynamicResource style1} ... </code></pre> <hr> <p>Currently if I do</p> <pre><code>&lt;UserControl.Resources&gt; &lt;Style x:Key="style1"&gt; &lt;!-- Place all styles in here --&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>It does not work.</p> <hr> <h2>Edit</h2> <p>Thanks to iltzortz answer I now have:</p> <p><strong>Dictionary1.xaml:</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Style TargetType="Grid"&gt; &lt;Setter Property="Background" Value="Green"&gt;&lt;/Setter&gt; &lt;/Style&gt; &lt;SolidColorBrush x:Key="Foo" Color="Red"&gt;&lt;/SolidColorBrush&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>Dictionary2.xaml:</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Style TargetType="Grid"&gt; &lt;Setter Property="Background" Value="Black"&gt;&lt;/Setter&gt; &lt;/Style&gt; &lt;SolidColorBrush x:Key="Foo" Color="Orange"&gt;&lt;/SolidColorBrush&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>MyUserControl:</strong></p> <pre><code>&lt;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"&gt; &lt;UserControl.Resources&gt; &lt;ResourceDictionary Source="Dictionary1.xaml" &gt;&lt;/ResourceDictionary&gt; &lt;/UserControl.Resources&gt; &lt;Grid &gt; &lt;Ellipse Fill="{DynamicResource Foo}" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>And I change resource dictionaries dynamically like this: <a href="https://stackoverflow.com/q/2381085/637142">switching wpf resource dictionaries at runtime</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload