serges_newj
serges_newj

Reputation: 815

How to define the same resource with another key?

If I have some resource defined in resources section of a control or in external resource dictionary, how can I define the same resource with another key? I mean how can I define resource entry that is just reference to another already existing entry?

To be more clear, I want to have several keys referencing one resource just like in programming language I can define several names for one constant. Is it possible?

I've tried that way

<Window.Resources>
    <SolidColorBrush x:Key="Brush1" Color="#FFDF7B04"/>

    <RadialGradientBrush x:Key="Brush2" GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
        <GradientStop Color="#5060FF40" Offset="0" />
        <GradientStop Color="#5060FF40" Offset="0.1" />
        <GradientStop Color="#3560FF40" Offset="0.4" />
        <GradientStop Color="#0860FF40" Offset="0.8" />
        <GradientStop Color="#0060FF40" Offset="1" />
    </RadialGradientBrush>

    <Style x:Key="CustomStyle" TargetType="UserControl">
        <Style.Resources>
            <StaticResource x:Key="TheBrush" ResourceKey="Brush1"/>
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="UserControl">
                    <Border x:Name="Border" BorderBrush="{StaticResource TheBrush}">
                        <!-- more content with several usings of {StaticResource TheBrush} -->
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

and it even works in design-time just like I need. But not even compiling.

UPD. To make more sense what I really want: In example above the reason why I need resource TheBrush is if sometimes I will decide to replace appearance of "TheBrush" in my style to another brush (say Brush2), I want to make such replacement in only one place. Withal I can't change definition of "Brush1" because it could be already used in many other controls (actually it could be placed in external resource dictionary and maintained by another person).

UPD 2. I am sorry that because of my poor English, I could not find the answer to my question yourself niether make my question correctly. Thanks to H.B. now I see that the key word of what I want is "alias". I'm looking for a way of aliasing resources, and there are many similar questions:

Alias or Reference in ResourceDictionary

Aliasing Resources (WPF)

Redefine/alias a resource in WPF?

etc.

So my question is just a duplicate and could be deleted.

Upvotes: 3

Views: 2133

Answers (2)

brunnerh
brunnerh

Reputation: 184506

You just have to use DynamicResource for you "renaming" instead of StaticResource, which may not work in all contexts, though.

Upvotes: 4

Logan
Logan

Reputation: 816

Try this:

<StackPanel Height="40" Orientation="Horizontal">
        <StackPanel.Resources>
            <Style x:Key="GenderButtonStyle" TargetType="{x:Type Button}">
                <Setter Property="MinWidth" Value="100" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border x:Name="InternalBorder"
                                    CornerRadius="5"
                                    Padding="5">
                                <TextBlock x:Name="InternalText"
                                           HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Text="{TemplateBinding Content}" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="Tag" Value="Male">
                                    <Setter TargetName="InternalBorder" Property="Background" Value="RoyalBlue" />
                                    <Setter TargetName="InternalText" Property="Foreground" Value="White" />
                                </Trigger>
                                <Trigger Property="Tag" Value="Female">
                                    <Setter TargetName="InternalBorder" Property="Background" Value="White" />
                                    <Setter TargetName="InternalBorder" Property="BorderBrush" Value="RoyalBlue" />
                                    <Setter TargetName="InternalBorder" Property="BorderThickness" Value="1,1,1,1" />
                                    <Setter TargetName="InternalText" Property="Foreground" Value="RoyalBlue" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <!-- Whatever you want -->
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <!-- Whatever you want -->
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
        <Button x:Name="MaleButton"
                Content="Male"
                Style="{StaticResource GenderButtonStyle}"
                Tag="Male" />
        <Button x:Name="FemaleButton"
                Margin="5,0,0,0"
                Content="Female"
                Style="{StaticResource GenderButtonStyle}"
                Tag="Female" />
    </StackPanel>

I utilized the sharing capabilities of styles to affect two buttons at the same time rather than repeating clunky code. Then inside the style of these buttons, I've taken control of the template and written my own one to look similar to what you're after.

This style can now be housed in a dictionary if you want.

Upvotes: 0

Related Questions