NiallUK
NiallUK

Reputation: 107

Make an image act like a button in WPF

I have the below stackpanel that features 2 images. One of a tick and one of an exit.

<StackPanel Grid.Row="3" Orientation="Horizontal">

    <Image Source="C:\Users\USER\Pictures\Tick.png" Height="100" Margin="55,10,0,5" HorizontalAlignment="Left" VerticalAlignment="Center" />
    <Image Source="C:\Users\USER\Pictures\Exit.png" Height="88" Margin="75,10,20,5" HorizontalAlignment="Right" VerticalAlignment="Center"/>

</StackPanel>

How am I able to make these 2 images act like a button? I know that you can use < button /> to create a new button, but I want to add a sort of property that will act as a button click when clicked.

Edit: I do not want the image to look like a button does with the outline and click effect. It just needs to be the image.

Upvotes: 0

Views: 71

Answers (2)

mm8
mm8

Reputation: 169420

You could completely customize the appearance of a button but still keep its functionality by creating a custom template:

<Button Click="Button_Click">
    <Button.Style>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Image Source="C:\Users\USER\Pictures\Tick.png" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

The above sample button should look exactly like an image but still be clickable like any other button.

Upvotes: 1

Matt Brown
Matt Brown

Reputation: 19

Why don't you just make the image the Content of a Button?

    <Button Height="100" Margin="55,10,0,5" HorizontalAlignment="Left" VerticalAlignment="Center">
        <Image Source="C:\Users\USER\Pictures\Tick.png"/>
    </Button>

Upvotes: 0

Related Questions