WPF 中未显示按钮的背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15089407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Background Image of Button not showing in WPF
提问by Андрей Про
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>
Image is showing as background in .xamlwhen 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?
当程序未运行时,图像在.xaml 中显示为背景,但当我运行应用程序时,图像不存在作为按钮的背景。我如何在按钮中调试这个背景?是否有任何愚蠢的错误?
回答by Shrinand
Let's make sure we have the following properties set right in your scenario
让我们确保我们在您的场景中正确设置了以下属性
1) Build Action -> Resource
1) 构建动作 -> 资源
2) Copy to Output Directory -> Do not copy
2) 复制到输出目录 -> 不要复制
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)
3)不要使用图片源的相对路径,尝试使用图片的完整路径这样)
<Image Source="pack://application:,,,/AssemblyNameContainingImageResource;component/Resource/button_picture.png" />
回答by Herks
You need to change the code like this
您需要像这样更改代码
<Button x:Name="mybutton" HorizontalAlignment="Left" Height="30" Margin="76,110,0,0" VerticalAlignment="Top" Width="25" Click="some_click">
<Image Source="Resource/button_picture.png"/>
</Button>
回答by Manish
Change the Build Action of button_picture.png to Resource if it is content. Also check the Copy to output directory property value
如果是内容,将button_picture.png 的Build Action 改为Resource。还要检查复制到输出目录属性值

