简体   繁体   中英

WPF button with imagebrush issues

Basically I would like to have image button.

<Button Command="{Binding Path=DisplayProductCommand}" >
   <Button.Background>
       <ImageBrush ImageSource="test.jpg"/>
   </Button.Background>
</Button>

By using the way above, I am facing an issue where the image doesn't cover whole button.

在此处输入图片说明

As shown above, this is the result and there is transparent spaces around. Please find below the original image:

在此处输入图片说明

What can I do to cover the whole button?

You need re-write controltemplate of button, replace default layout by removing ButtonChrome or you can set BorderThickness property of button to zero.

<Button Command="{Binding Path=DisplayProductCommand}" BorderThickness="0">
   <Button.Background>
       <ImageBrush ImageSource="test.jpg"/>
   </Button.Background>
</Button>

As @geedub mentioned, you can just define a colour gradient to match that image:

<Button Content="Look at my background" Padding="10,5">
    <Button.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0" Color="#FFE5E5E5" />
            <GradientStop Offset="1" Color="#FFBABED3" />
        </LinearGradientBrush>
    </Button.Background>
</Button>

If this is not acceptable for your requirements then I would advise that you crop your image to remove the shadow effect that chao wang mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM