C# 按钮内的图标?

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

Icon inside of button?

c#

提问by alexy12

How do i add an icon like in the screenshot below inside of a button? I cannot seem to find how to do it.

如何在按钮内添加如下图所示的图标?我似乎无法找到如何去做。

http://i.stack.imgur.com/6HGcK.jpg

http://i.stack.imgur.com/6HGcK.jpg

回答by marc wellman

WPF (and Silverlight) is offering a control called Image. As the name implies it's a container which can hold an image inside. Use such a control to represent the icon you want and then place it inside your Buttonthrough its Contentproperty

WPF(和 Silverlight)提供了一个名为Image. 顾名思义,它是一个可以在内部容纳图像的容器。使用这样的控件来表示你想要的图标,然后Button通过它的Content属性把它放在你的里面

like here:

像这儿:

<Button x:Name="btn_ControlRun">
    <StackPanel Orientation="Horizontal">
        <Image Stretch="Fill" Source="right.png"/>
        <Label Content="Start Tasks" />
    </StackPanel>
</Button>

回答by kmatyaszek

Try this:

尝试这个:

    <Button>
        <StackPanel Orientation="Horizontal">
            <Image Source="/Image/ok.png" />
            <TextBlock Text="Start Tasks" />
        </StackPanel>
    </Button>

回答by Ria

In WinForms use Button.Image(MSDN) like this:

在 WinForms 中使用Button.Image( MSDN) 像这样:

private void SetMyButtonIcon()
 {
    // Assign an image to the button.
    button1.Image = Image.FromFile("C:\Graphics\My.ico");
    // Align the image and text on the button.
    button1.ImageAlign = ContentAlignment.MiddleRight;    
    button1.TextAlign = ContentAlignment.MiddleLeft;
 }

and you can use Button.TextImageRelationProperty to set the position of text and imagerelative to each other:

并且您可以使用Button.TextImageRelation属性来设置文本和图像相对于彼此的位置

  • Overlay: image and text share the same space on a control.
  • ImageBeforeText: the image is displayed horizontally before the text of a control.
  • TextBeforeImage: the text is displayed horizontally before the image of a control.
  • ImageAboveText: the image is displayed vertically above the text of a control.
  • TextAboveImage: the text is displayed vertically above the image of a control.
  • Overlay:图像和文本在控件上共享相同的空间。
  • ImageBeforeText:图像在控件文本之前水平显示。
  • TextBeforeImage:文本在控件图像之前水平显示。
  • ImageAboveText:图像垂直显示在控件文本的上方。
  • TextAboveImage:文本垂直显示在控件图像的上方。

回答by Dozer789

If you put the button on the form, you can right click on it and click properties and then go to "Image" and put in the image you want, them go to the bottom and click "TextImageRelation" and click the drop down menu then click "ImageBeforeText", you can make it however you want but i personally like image before text the best.

如果将按钮放在表单上,​​则可以右键单击它并单击属性,然后转到“图像”并输入所需的图像,然后转到底部并单击“TextImageRelation”,然后单击下拉菜单单击“ImageBeforeText”,您可以随心所欲地制作它,但我个人最喜欢文本前的图像。

Hope this helps.

希望这可以帮助。