2


I'm trying to change the background color of mahapp button when the mouse goes over it. The problem is that if I use the code below:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="DarkGoldenrod"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Found in this answer the color does change but my button "forget" to be a mahapp button, i.e. it loses the style.
How to change the color without losing the style?
Thank you

2
  • 2
    try provide base style: <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"> Commented May 18, 2017 at 13:54
  • when I modified other properties like Font family stating <Style TargetType="{x:Type Button}" BasedOn="{StaticResource MetroButton}"> prevented this kind of issue, that time doesn't work Commented May 18, 2017 at 14:06

2 Answers 2

3

Try the following:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
    <Button.Style>
        <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Aqua"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>
Sign up to request clarification or add additional context in comments.

3 Comments

In this way the style remains the same but the color isn't changed, that's way I use template
This is the correct answer to the question, much simpler than redefining template. Don't know why people downvote
This actually did not work for me. @mm8's solution worked though.
2

How to change the color without losing the style?

You need to re-define the entire template:

<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="50" Height="50" 
                HorizontalContentAlignment="Left" BorderBrush="{x:Null}" Margin="50,0,0,0">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{DynamicResource FlatButtonBackgroundBrush}" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="FontSize" Value="{DynamicResource FlatButtonFontSize}" />
            <Setter Property="Foreground" Value="{DynamicResource FlatButtonForegroundBrush}" />
            <Setter Property="Padding" Value="10 5 10 5" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}" xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
                        <Border x:Name="Border"
                            Margin="0"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Controls:ContentControlEx x:Name="PART_ContentPresenter"
                                                   Padding="{TemplateBinding Padding}"
                                                   HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                   VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                                   Content="{TemplateBinding Content}"
                                                   ContentCharacterCasing="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Controls:ControlsHelper.ContentCharacterCasing)}"
                                                   ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                                   ContentTemplate="{TemplateBinding ContentTemplate}"
                                                   ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                                   RecognizesAccessKey="True"
                                                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGoldenrod" />
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="{DynamicResource FlatButtonPressedBackgroundBrush}" />
                    <Setter Property="Foreground" Value="{DynamicResource FlatButtonPressedForegroundBrush}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{DynamicResource GrayBrush2}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

You cannot override only a part of a ControlTemplate:

WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?

Comments

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.