Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an icon into a button (MahApps)

I want to put a icon from the MahApps library into a normal button. I tried it this way:

<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?

like image 595
Christian Klemm Avatar asked Apr 06 '16 16:04

Christian Klemm


3 Answers

You have 2 options.

First you can use the Icon rersources (sample with 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.

like image 58
punker76 Avatar answered Oct 11 '22 06:10

punker76


As you can see in the github example, they put a Rectangle inside the Button and then use the OpacityMask property.

<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>

Source

like image 24
ganchito55 Avatar answered Oct 11 '22 05:10

ganchito55


You can try the following ... just as suggested by @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>
like image 27
aggietech Avatar answered Oct 11 '22 06:10

aggietech