Reputation: 45
How to make a buttons auto-size with an image instead of fixing a default button size? Thanks really appreciated.
All my buttons are fixed size, I need to have some buttons buttons to be big, some small depending on the image size.
heres my codes:
Button tba = new Button();
tba.FontSize = 19;
tba.Height = 300
tba.MinWidth = 100;
//tba.Height = Double.NaN;
//tba.Width = Double.NaN;
ImageBrush brush = new ImageBrush();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
brush.ImageSource = bitmap;
tba.Background = brush;
wrapPanel1.Children.Add(tba);
Upvotes: 0
Views: 218
Reputation: 11426
To size the button to the image: host the image in an Image
and set Image.Strech
to None and remove the sizes from your Button
:
Button tba = new Button();
Image myImage = new Image();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"files.png" + lstQuestion[i].ImageURL.Substring(1), UriKind.Absolute);
bitmap.EndInit();
myImage.Source = myBitmapImage;
myImage.Stretch = Stretch.None;
tba.Content = myImage;
If you want more than just an image in your Button
add the image to panel that will let the image fill the space, e.g. a Grid
, then set Button.Content
to that panel.
Incidentally in XAML you can just go:
<Button>
<Image Strech="None" Source=".\Water Lilies.jpg"/>
</Button>
Upvotes: 1