wpf 如何将图标放入按钮中 (MahApps)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36456396/
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
How to put an icon into a button (MahApps)
提问by Christian Klemm
I want to put a icon from the MahApps library into a normal button. I tried it this way:
我想将 MahApps 库中的图标放入普通按钮中。我是这样试的:
<Button Height="20" Width="25" Background="Transparent" Foreground="White" Content="{StaticResource appbar_close}"/>
Which ended like this:
结局是这样的:
So how can I integrate this icon into this button in a suitable position?
那么如何将这个图标整合到这个按钮的合适位置呢?
回答by punker76
You have 2 options.
您有 2 个选择。
First you can use the Icon rersources (sample with MetroCircleButtonStyle)
首先,您可以使用图标资源(带有 MetroCircleButtonStyle 的示例)
<Button Width="50"
Height="50"
Style="{DynamicResource MetroCircleButtonStyle}">
<Rectangle Width="20"
Height="20"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{DynamicResource appbar_close}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
and second the new Icon packs
其次是新的图标包
<Button Width="50"
Height="50"
Style="{DynamicResource MetroCircleButtonStyle}">
<Controls:PackIconModern Width="20" Height="20" Kind="Close" />
</Button>
Hope that helps.
希望有帮助。
回答by ganchito55
As you can see in the github example, they put a Rectangleinside the Buttonand then use the OpacityMaskproperty.
正如您在github 示例中所见,他们在Button内放置了一个Rectangle,然后使用OpacityMask属性。
<ToggleButton Width="50"
Height="50"
IsEnabled="False"
Style="{DynamicResource MetroCircleToggleButtonStyle}">
<Rectangle Width="20"
Height="20"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ToggleButton}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill"
Visual="{DynamicResource appbar_city}" />
</Rectangle.OpacityMask>
</Rectangle>
回答by aggietech
You can try the following ... just as suggested by @ganchito55
您可以尝试以下...正如@ganchito55 所建议的那样
<Button Width="25" Height="20">
<Button.Content>
<Rectangle Width="20" Height="20">
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_close}" Stretch="None" />
</Rectangle.Fill>
</Rectangle>
</Button.Content>
</Button>
回答by Fam Wired
In my application I used this code before to create lots of button's with icons:
在我的应用程序中,我之前使用此代码创建了许多带有图标的按钮:
<Button Command="{Binding EditDetailCommand}" ToolTip="Edit" Style="{DynamicResource buttonstyle}">
<metro:PackIconModern Width="Auto" Height="Auto" Kind="Edit" />
</Button>
It seems not to work with the latest MahApps anymore?
它似乎不再适用于最新的 MahApps?
I really liked the simple syntax as I think it's more than enough to fill up my xaml files.
我真的很喜欢简单的语法,因为我认为它足以填满我的 xaml 文件。
回答by Ksdmg
I wanted to have the Icon only as a clickable control and this is how it works for me with latest Mahapps version:
我只想将图标作为可点击的控件,这就是它在最新的 Mahapps 版本中的工作方式:
<Button Width="16"
Height="16"
Padding="0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Click="HelpButton_Click"
Style="{x:Null}"
Background="Transparent"
BorderThickness="0"
>
<Button.Content>
<Icons:PackIconMaterial Kind="Help"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Button.Content>
</Button>
Hope this helps someone
希望这有助于某人


