0

I have a button that defines a custom Theme Dictionary.

<Button Content="Expand Slot"
        FontWeight="SemiBold"
        Command="{Binding ElementName=ThisPage, Path=ViewModel.NavigateToSlot, Mode=OneWay}"
        CommandParameter="{x:Bind SlotNumber, Mode=OneWay}">
    <Button.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Light">
                    <!--Default colors-->
                    <SolidColorBrush x:Key="ButtonBackground" Color="Transparent"/>
                    <SolidColorBrush x:Key="ButtonForeground" Color="DarkSlateBlue"/>
                    <SolidColorBrush x:Key="ButtonBorderBrush" Color="DarkSlateBlue"/>

                    <!--Mouseover colors-->
                    <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent"/>
                    <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="MediumSlateBlue"/>
                    <SolidColorBrush x:Key="ButtonBorderBrushPointerOver" Color="MediumSlateBlue"/>

                    <!--Colors while being clicked-->
                    <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="Transparent"/>
                    <SolidColorBrush x:Key="ButtonForegroundPressed" Color="MediumSlateBlue"/>
                    <SolidColorBrush x:Key="ButtonBorderBrushPressed" Color="MediumSlateBlue"/>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>
        </ResourceDictionary>
    </Button.Resources>
</Button>

This works fine but I want to be able to use the same Dictionary across multiple buttons without having to edit each button's <Button.Resources>

<Button Content="Button 1">
    <Button.Resources>
        -- Apply my colors --
    </Button.Resources>
</Button>

<Button Content="Button 2">
    <Button.Resources>
        -- Apply my colors --
    </Button.Resources>
</Button>

What is the correct way to do this in UWP?

1 Answer 1

0

You could put the ResourceDictionary into the Page.Resources so that all the buttons on that page would use this style. If you want to apply this style to all the buttons in the app, then you could put the ResourceDictionary inside the Application.Resources in the App.xaml file.

Like this:

 <Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Light">
                <!--Default colors-->
                <SolidColorBrush x:Key="ButtonBackground" Color="Transparent"/>
             
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

Update:

If you want to apply to certain buttons, it would be better to use custom styles. You could create a default style of Button and change the properties to what you want. You just need to find out which property is using the resources like ButtonBackground and ButtonBackgroundPointerOver in the style. Then you could change the value of these properties to the value you want.

Here is the style that I've changed and you could directly use it. It has the same behavior as the ResourceDictionary.

        <Style TargetType="Button" x:Key="TestKey">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BackgroundSizing" Value="OuterBorderEdge" />
        <Setter Property="Foreground" Value="DarkSlateBlue" />
        <Setter Property="BorderBrush" Value="DarkSlateBlue" />
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
        <Setter Property="Padding" Value="{StaticResource ButtonPadding}" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
        <Setter Property="FontWeight" Value="Normal" />
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
        <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
        <Setter Property="FocusVisualMargin" Value="-3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter x:Name="ContentPresenter"
          Background="{TemplateBinding Background}"
          BackgroundSizing="{TemplateBinding BackgroundSizing}"
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}"
          Content="{TemplateBinding Content}"
          ContentTemplate="{TemplateBinding ContentTemplate}"
          ContentTransitions="{TemplateBinding ContentTransitions}"
          CornerRadius="{TemplateBinding CornerRadius}"
          Padding="{TemplateBinding Padding}"
          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
          AutomationProperties.AccessibilityView="Raw">

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal">

                                    <Storyboard>
                                        <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                    </Storyboard>
                                </VisualState>

                                <VisualState x:Name="PointerOver">

                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="MediumSlateBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="MediumSlateBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                    </Storyboard>
                                </VisualState>

                                <VisualState x:Name="Pressed">

                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="MediumSlateBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="MediumSlateBlue" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerDownThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                    </Storyboard>
                                </VisualState>

                                <VisualState x:Name="Disabled">

                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>

                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>
                    </ContentPresenter>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

You could put this style in Page.Resources or Application.Resources, then use it like the following:

 <Button Content="Expand Slot" FontWeight="SemiBold" Style="{StaticResource TestKey}" />

You could get more information about custom style here: Create custom styles

Sign up to request clarification or add additional context in comments.

1 Comment

I want to be able to apply these colors to specific buttons - not an implicit resource for all buttons on the page. Also, I tried using it in the Page.Resources but that gives a compiler error: This Member Resources has more than one item, use the Items property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.