在你的情况下,这很容易!
将图像作为资源添加到您的项目中,然后在 XAML 中使用如下内容:
<Button HorizontalAlignment="Left" Margin="20,0,0,20" VerticalAlignment="Bottom" Width="50" Height="25">
<Image Source="image.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
或者,更复杂的方式:
如果您使用 MVVM Pattern,您可以执行以下操作
在您的 XAML 中:
<Button Focusable="False" Command="{Binding CmdClick}" Margin="0">
<Image Source="{Binding ButtonImage}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
</Image>
</Button>
在您的视图模型中:
private Image buttonImage;
public Image ButtonImage
{
get
{
return buttonImage;
}
}
在您的 ViewModel 的构造函数或它的初始化中的某处:
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("image.png", UriKind.Relative);
src.CacheOption = BitmapCacheOption.OnLoad;
src.EndInit();
buttonImage = new Image();
buttonImage.Source = src;