添加图像中的WPF按钮 [英] Add an Image in a WPF button
问题描述
我试过这个解决方案:
<Button>
<StackPanel>
<Image Source="Pictures/img.jpg" />
<TextBlock>Blablabla</TextBlock>
</StackPanel>
</Button>
但我可以看到只有在项目窗口中的图像,当我启动该程序消失。
But I can see the image only in the project window, when I launch the program it disappears.
如果我试试这个:
Image img = new Image();
img.Source = new BitmapImage(new Uri("foo.png"));
StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(10);
stackPnl.Children.Add(img);
Button btn = new Button();
btn.Content = stackPnl;
我得到异常的,在presentationFramework.dll'System.Windows.Markup.XamlParseException'。
I get a "'System.Windows.Markup.XamlParseException' in PresentationFramework.dll" exception.
你能帮我想想办法?
谢谢你。
Can you help me find a solution? Thanks.
推荐答案
在一个丢失的形象有几个事情要考虑的情况:
In the case of a 'missing' image there are several things to consider:
-
在XAML中无法找到它可能会忽略它的资源(当它不会抛出
XamlParseException
)
资源必须适当增加,并定义为:
The Resource must be properly added and defined:
-
确保它存在于你的项目,其中的预期。
确保它是建立与您的项目资源。
的(右键 - >属性 - > BuildAction的='资源')的
另一件事在类似案件中尝试,这也是有用图像的重复使用(或任何其他资源):
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
定义你的形象在你的XAML中的资源:
<UserControl.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
和以后使用它在你的期望的控制(S):
And later use it in your desired control(s):
<Button Content="{StaticResource MyImage}" />
这篇关于添加图像中的WPF按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!