Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image to a button WPF

Tags:

c#

wpf

I understand that i can overlay an image on a button, but is it possible that a button can take the exact size of an image? In other words, i want to get rid of the rectangular shaped button and adapt it to the size and shape of the image.

like image 661
user720694 Avatar asked Jan 26 '26 13:01

user720694


2 Answers

You can set the Template of button like below:

    <Button >
        <Image Source="pan-left.png"/>
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <ContentPresenter Content="{TemplateBinding Content}"/>
            </ControlTemplate>
        </Button.Template>
    </Button>

TemplateBinding is used to set the Content property in order to set the Content inside the ControlTemplate, as you can move this ControlTemplate to the Resources and can use it for multiple button and you can set the content on button itself

like image 105
Nitin Avatar answered Jan 29 '26 02:01

Nitin


Check this Question.

You can re-template the button.

<Button x:Name="btn16x16">
    <Button.Template>
        <ControlTemplate>
            <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                <Image Source="pack://siteoforigin:,,,/Resources/SixteenBySixteen.png" 
                       Width="16" 
                       Height="16"/>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!