【问题标题】:Icon inside of button?按钮内的图标?
【发布时间】:2012-12-27 23:00:14
【问题描述】:

如何在按钮内添加如下屏幕截图中的图标?我似乎找不到怎么做。

【问题讨论】:

  • 感谢 Dante 添加图片 :)
  • WinForms, WPF, web???
  • @DanielMošmondor 或者我们可以给出 3 个答案,每个框架 1 个。很高兴来自谷歌并找到答案。

标签: c#


【解决方案1】:

在 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;
 }

您可以使用Button.TextImageRelation 属性来设置position of text and image 相对于彼此:

  • Overlay:图像和文本在控件上共享相同的空间。
  • ImageBeforeText:图像在控件文本之前水平显示。
  • TextBeforeImage:文本水平显示在控件图像之前。
  • ImageAboveText:图像垂直显示在控件文本的上方。
  • TextAboveImage:文本垂直显示在控件图像上方。

【讨论】:

  • 如果你把 Text 和 Image Alignment 都放在 MiddleLeft 并在 Text 属性前面放一些空格字符,可以使按钮自动调整大小并动态更改其文本以使其完美契合,并且图片不会覆盖文本按钮。
【解决方案2】:

如果您将按钮放在表单上,​​您可以右键单击它并单击属性,然后转到“图像”并放入您想要的图像,它们会转到底部并单击“TextImageRelation”并单击下拉下拉菜单然后点击“ImageBeforeText”,你可以随意制作,但我个人最喜欢文字前的图像。

希望这会有所帮助。

【讨论】:

  • 是的,它可以工作,但是如何调整图像大小以适应/拉伸?
【解决方案3】:

试试这个:

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

【讨论】:

    【解决方案4】:

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

    喜欢这里:

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

    【讨论】:

    • 次要问题,与 OP 的要求相比,您的标签和图像位置颠倒了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2021-05-14
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多