2

I have a Button in XAML

<Button x:Name="btnclose" Click="btnclose_Click">
    <Viewbox StretchDirection="Both">
        <Image x:Name="imgclosebtn" Source="images/blackclosebtn.png" Margin="2"/>
    </Viewbox>
</Button>

when I hover over the button I want to change the source of the image. How can I do this? I have already changed the backgroundcolor on hover:

<Style TargetType="Button">
    <Setter Property="Background" Value="LightGray"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="0">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

I have already tried it with TargetName and a Setter but it is out of scope

I preferably would like to do it in XAML, but C# is also ok

MP13
  • 390
  • 3
  • 11
  • Does this answer your question? https://stackoverflow.com/questions/17259280/how-do-you-change-background-for-a-button-mouseover-in-wpf – Andy Jul 20 '20 at 08:30
  • No. He changes the Backgroundcolor of the button and I want to change the source of the image which is a child of the button – MP13 Jul 20 '20 at 08:33
  • Remove the Viewbox. It is redundant, since Image already supports stretching. – Clemens Jul 20 '20 at 08:33

0 Answers0