开发者

Change button background

开发者 https://www.devze.com 2023-01-27 03:35 出处:网络
After several attempts to change the backg开发者_如何学Pythonround of a button, and getting errors of casting and things like that I finnaly got to this point:

After several attempts to change the backg开发者_如何学Pythonround of a button, and getting errors of casting and things like that I finnaly got to this point:

Uri dir = new Uri("red_flag.png",UriKind.RelativeOrAbsolute);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
ImageBrush bru = new ImageBrush();
bru.ImageSource=source;
bru.Opacity = 100;

This code does not generate errors, but I can't see the changes when I call:

button1.background = bru;

It just makes anything! :(


found the answer myelf after reading Mick's answer, I share with you what I did:

Uri dir = new Uri("red_flag.png", UriKind.Relative);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
Image image = new Image();
image.Source = source;
StackPanel stack = new StackPanel();
stack.Children.Add(image);
myButton.Content = stack;

Thanks for your help

Update 1: For best results set the padding property of your button to 0 (in each of the cases) so the image can resize automatically to fill all the button, please note this could hide your actual content, in my case this was what I wanted.


If this code is part of the click event handler for the same button you will have this problem.

Peter Torr explains why here and offers a solution.

Why can't I change the Background of my Button on a Click event?

0

精彩评论

暂无评论...
验证码 换一张
取 消