C# 在 WPF 按钮中添加图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17515631/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:42:30  来源:igfitidea点击:

Add an image in a WPF button

c#wpfimagebutton

提问by Marco

I tried this solution:

我试过这个解决方案:

<Button>
    <StackPanel>
        <Image Source="Pictures/img.jpg" />
        <TextBlock>Blablabla</TextBlock>
    </StackPanel>
</Button>

But I can see the image only in the project window, and when I launch the program it disappears.

但是我只能在项目窗口中看到图像,当我启动程序时它消失了。

If I try this:

如果我试试这个:

  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;

I get a "'System.Windows.Markup.XamlParseException' in PresentationFramework.dll" exception.

我在 PresentationFramework.dll 中收到“'System.Windows.Markup.XamlParseException'”异常。

What is the solution?

解决办法是什么?

采纳答案by Ron.B.I

In the case of a 'missing' image there are several things to consider:

在“丢失”图像的情况下,需要考虑以下几点:

  1. When XAML can't locate a resource it might ignore it (when it won't throw a XamlParseException)

  2. The resource must be properly added and defined:

    • Make sure it exists in your project where expected.

    • Make sure it is built with your project as a resource.

      (Right click → Properties → BuildAction='Resource')

  1. 当 XAML 找不到资源时,它可能会忽略它(当它不会抛出XamlParseException

  2. 必须正确添加和定义资源:

    • 确保它存在于您的项目中的预期位置。

    • 确保它是使用您的项目作为资源构建的。

      (右键单击 → 属性 → BuildAction='Resource')

Snippet

片段

Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):

在类似情况下尝试的另一件事,这对于重用图像(或任何其他资源)也很有用:

Define your image as a resource in your XAML:

将图像定义为 XAML 中的资源:

<UserControl.Resources>
     <Image x:Key="MyImage" Source.../>
</UserControl.Resources>

And later use it in your desired control(s):

稍后在您想要的控件中使用它:

<Button Content="{StaticResource MyImage}" />

回答by Vimal CK

Please try the below XAML snippet:

请尝试以下 XAML 代码段:

<Button Width="300" Height="50">
  <StackPanel Orientation="Horizontal">
    <Image Source="Pictures/img.jpg" Width="20" Height="20"/>
    <TextBlock Text="Blablabla" VerticalAlignment="Center" />
  </StackPanel>
</Button>

In XAML elements are in a tree structure. So you have to add the child control to its parent control. The below code snippet also works fine. Give a name for your XAML root grid as 'MainGrid'.

在 XAML 中,元素位于树结构中。所以你必须将子控件添加到它的父控件中。下面的代码片段也可以正常工作。为您的 XAML 根网格命名为“MainGrid”。

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;
MainGrid.Children.Add(btn);

回答by Maciek

Use:

用:

<Button Height="100" Width="100">
  <StackPanel>
    <Image Source="img.jpg" />
    <TextBlock Text="Blabla" />
  </StackPanel>
</Button>

It should work. But remember that you must have an image added to the resource on your project!

它应该工作。但请记住,您必须将图像添加到项目的资源中!

回答by kjhf

You can set the button's background to the image if you then want to overlay text.

如果您想覆盖文本,您可以将按钮的背景设置为图像。

<Button>
   <Button.Background>
     <ImageBrush ImageSource="/AssemblyName;component/Pictures/img.jpg"/>
   </Button.Background>
   <TextBlock>Blablabla</TextBlock>
</Button>

Watch out for the image source syntax. See this questionfor help.

注意图像源语法。请参阅此问题以获取帮助。

回答by yu yang Jian

Try ContentTemplate:

尝试内容模板:

<Button Grid.Row="2" Grid.Column="0" Width="20" Height="20"
        Template="{StaticResource SomeTemplate}">
    <Button.ContentTemplate>
        <DataTemplate>
            <Image Source="../Folder1/Img1.png" Width="20" />
        </DataTemplate>
    </Button.ContentTemplate>
</Button>

回答by Ali Asad

I also had the same issue. I've fixed it by using the following code.

我也有同样的问题。我已经使用以下代码修复了它。

        <Button Width="30" Margin="0,5" HorizontalAlignment="Stretch"  Click="OnSearch" >
            <DockPanel>
                <Image Source="../Resources/Back.jpg"/>
            </DockPanel>
        </Button>

Note:Make sure the build action of the image in the property window, should be Resource.

注意:确保属性窗口中图像的构建操作应为Resource.

enter image description here

在此处输入图片说明

回答by Robin Bennett

I found that I also had to set the Access Modifier in the Resources tabto 'Public' - by default it was set to Internal and my icon only appeared in design mode but not when I ran the application.

我发现我还必须将“资源”选项卡中的“访问修饰符”设置为“公共”——默认情况下它设置为“内部”,我的图标仅出现在设计模式下,但在我运行应用程序时不会出现。