wpf 使用 Visual Studio 将图标添加到按钮

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

Add icon to button with Visual Studio

c#wpfvisual-studioxaml

提问by tempname11112

I have a simple question. I want to add an icon to a C# WPF Button control. I do not want to have to write C# code, or edit XAML to do this.

我有一个简单的问题。我想向 C# WPF 按钮控件添加一个图标。我不想编写 C# 代码或编辑 XAML 来执行此操作。

However, when I click on the button in the Designer, there is no option under properties to set an image. How do you do this through the Visual Studio GUI?

但是,当我单击设计器中的按钮时,属性下没有设置图像的选项。您如何通过 Visual Studio GUI 执行此操作?

回答by BradleyDotNET

The easiest/best way to do this is to add an Imagecontrol as the Contentof the Button.

要做到这一点,最简单的/最好的办法是增加一个Image控制作为ContentButton

The property window is somewhat limited in what it can do, and only supports text for that property. This does include bindings, so you could use an ImageStaticResource. I couldn't find an easy way to create one from the property designer either though.

属性窗口的功能有限,仅支持该属性的文本。这确实包括绑定,因此您可以使用ImageStaticResource. 不过,我也找不到一种简单的方法来从属性设计器中创建一个。

So basically, you are stuck with editing XAML. Either with a direct Contentproperty or by creating an element in ResourcesIts not that bad! Just write:

所以基本上,您一直在编辑 XAML。无论是使用直接Content属性还是通过在Resources它的中创建元素都没有那么糟糕!写就好了:

<Button>
   <Button.Content>
      <Image ImageSource="..."/>
   </Button.Content>
</Button>

Now of course, you could create a custombutton that exposed that property via the designer, but thats even moreXAML. Its WPF, you are going to have to write XAML, so learning how should be a priority.

当然,现在您可以创建一个自定义按钮,通过设计器公开该属性,但这更多的是XAML。它的 WPF,你将不得不编写 XAML,所以学习如何应该是一个优先事项。

回答by user5718778

Visual Studio 2015: create a button. create an image. set the source of the image to the right file of your resources (e.g. a .png file that you included to your project) drag the image over the button. A text shows that asks you to press ALT to replace the text of the button with the image.

Visual Studio 2015:创建一个按钮。创建图像。将图像源设置为资源的正确文件(例如,您包含在项目中的 .png 文件)将图像拖到按钮上。文本显示要求您按 ALT 以用图像替换按钮的文本。

I currently don't know how to get both, image and text, for a button.

我目前不知道如何同时获取按钮的图像和文本。

in XAML, it looks like this:

在 XAML 中,它看起来像这样:

<Button x:Name="button12" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75">
   <Image x:Name="image" HorizontalAlignment="Left" Height="24" VerticalAlignment="Top" Width="24" Source="gfx/new.png"/>
</Button>

回答by Abbas Akhtar

you could install FontAwesomeon your Computer

你可以安装FontAwesome在你的电脑上

Goto http://fontawesome.ioand download. Open the downloaded ZIP file and inside the font folder there should be .otf file, Install that!

转到http://fontawesome.io并下载。打开下载的 ZIP 文件,字体文件夹内应该有 .otf 文件,安装它!

and in the content of the button you could simply type the desired icons code!

在按钮的内容中,您只需输入所需的图标代码即可!

For eg: if you want your button to look like this

例如:如果你想让你的按钮看起来像这样

then install FontAwesome Font and in your button tag type Content="&#xf01e;" FontFamily="FontAwesome"

然后安装 FontAwesome Font 并在您的按钮标签类型中 Content="&#xf01e;" FontFamily="FontAwesome"

More codes can be found here http://fontawesome.io/cheatsheet/

更多代码可以在这里找到http://fontawesome.io/cheatsheet/