Reputation: 2381
I'm trying to put a image as a button background, i see a lot of questions about this, and the better way seems to be subclassing button to create a custom ImageButton.
Anyway i tried using ImageBrush to set the image as button background, but it isn't working, something like this:
Button b = new Button();
ImageBrush ib = new ImageBrush();
ib.ImageSource = home;
b.Background = ib;
And setting an image as content:
Image i = new Image();
i.Source = home;
Button b = new Button();
b.Content = home;
Then i saw a ImageButton class, i downloaded and added to project.
But i cant compile it
static ImageButton() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}
public static readonly DependencyProperty ImageSizeProperty =
DependencyProperty.Register("ImageSize", typeof(double), typeof(ImageButton),
new FrameworkPropertyMetadata(30.0, FrameworkPropertyMetadataOptions.AffectsRender));
Basically i'm getting this errors:
'System.Windows.DependencyProperty' does not contain a definition for 'OverrideMetadata' and no extension method 'OverrideMetadata' accepting a first argument of type 'System.Windows.DependencyProperty' could be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'FrameworkPropertyMetadata' could not be found (are you missing a using directive or an assembly reference?)
The name 'FrameworkPropertyMetadataOptions' does not exist in the current context
Someone could tell me how to solve this? or if i'm doing something wrong putting the background image or a way to make it, just need a button with a image filling it, without padding or margin or borders.
Upvotes: 1
Views: 2169
Reputation: 9434
You could simply use the ImageBrush
.
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("ms-appx:/Assets/SmallLogo.png"));
Button1.Background = brush;
Have a look at this thread.
Hope it helps!
Upvotes: 0
Reputation: 8161
If you just want a button with an image as a background, try this
<Grid>
<Button BorderThickness="0">
<Button.Background>
<ImageBrush Stretch="None" ImageSource="your_image.jpg"/>
</Button.Background>
</Button>
<Grid>
Upvotes: 2