WPF 中未显示按钮的背景图像 [英] Background Image of Button not showing in WPF
问题描述
我的程序中有很多按钮.每个按钮的背景设置为
I have program in which I have lots of buttons. Each of button has background set as
<Button x:Name="mybutton" HorizontalAlignment="Left" Height="30" Margin="76,110,0,0" VerticalAlignment="Top" Width="25" Click="some_click">
<Button.Background>
<ImageBrush ImageSource="Resource/button_picture.png"/>
</Button.Background>
</Button>
当程序没有运行时,图像在 .xaml 中显示为背景,但当我运行应用程序时,图像不存在作为按钮的背景.我如何在按钮中调试这个背景?是否有任何愚蠢的错误?
Image is showing as background in .xaml when program not running but when i run application image is not there as background of button. How do i debug this background in button ? Is there any goofy error that is there?
推荐答案
让我们确保我们在您的场景中正确设置了以下属性
Let's make sure we have the following properties set right in your scenario
1) 构建操作 -> 资源
1) Build Action -> Resource
2) 复制到输出目录 -> 不要复制
2) Copy to Output Directory -> Do not copy
3) 不使用图片源的相对路径,尝试使用图片的完整路径这样在 WPF 中正常)
3) Instead of using the relative path for image source, try using a full path to the image like this (I say this because I don't know where the image resource is located in your project, using relative path is perfectly normal in WPF)
<Image Source="pack://application:,,,/AssemblyNameContainingImageResource;component/Resource/button_picture.png" />
这篇关于WPF 中未显示按钮的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!