Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add Resources or a ResourceDictionary to a Style?

Is it possible to define a ResourceDictionary in a Style?

For example, suppose I wanted to have two different Styles for StackPanels and in one I want all the buttons to be blue and the other I want them to be red. Is this possible?

Something like

<Style x:Key="RedButtonsPanel" TargetType="{x:Type StackPanel}">
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="StackPanel.Resources">
        <Setter.Value>
            <ResourceDictionary>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Red" />
                </Style>
            </ResourceDictionary>
        </Setter.Value>
    </Setter>
</Style>

The above code fails with an error about the Property value of a Setter cannot be null (even though it's obviously not null).

I can do something like

<ResourceDictionary x:Key="RedButtons">
    <Style TargetType="{x:Type Button}">
        <Setter Property="Width" Value="100" />
        <Setter Property="Background" Value="Red" />
    </Style>
</ResourceDictionary>

<StackPanel Resources={StaticResource RedButtons} />

However I was wondering if there was a way to merge the ResourceDictionary into the style.

like image 340
Rachel Avatar asked Nov 22 '10 20:11

Rachel


1 Answers

StackPanel.Resources is not a DependencyProperty and therefore I don't believe you will be able to set that property within the style.

like image 180
Aaron McIver Avatar answered Oct 08 '22 01:10

Aaron McIver