Creating Metro Style Circle Button Using Mahapps Metro UI Framework

In this article, I will explain how to create Metro style Circle Button in WPF application using Mahapps Metro UI framework and Microsoft Visual Studio.

Step 1

For installation of Metro UI framework, please check my previous blog.
Step 2

Create a new button in MainWindow, like Image 1.



I have added three buttons in this project. You can add these from ToolBox.

Step 3

We have to install the Icon Resource package for control customization using below command, because MahApps.Metro does not embed any Icons Package.

Install-Package MahApps.Metro.Resources

For installation of MahApps.Metro.Resources, click TOOLS - NuGet Package Manager - Package Manager Console.



Enter the above command in Package Manager Console, then hit the Enter button.

After a few seconds, you will get an installation completion message.



Step 4

Add the blow ResourceDictionary in App.xaml file.
  1. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />   
  2. <ResourceDictionary Source="/Resources/Icons.xaml" />   


Step 5

Add the below code into button opening tag.
  1. <Rectangle Width="20" Height="20" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">   
  2. <Rectangle.OpacityMask>   
  3. <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_disk}" />   
  4. </Rectangle.OpacityMask>   
  5. </Rectangle>   


I have applied the same logic for all the three buttons. You can use it for remaining buttons.

You can change the button icon from Visual Property through applying static resources.



Complete MainWindow XAML source
  1. <Controls:MetroWindow x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" mlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:local="clr-namespace:WpfApplication7" mc:Ignorable="d" itle="MainWindow" Height="350" Width="525">  
  2.     <Grid>  
  3.         <Button ToolTip="Add a new employee record" x:Name="button" Style="{DynamicResource MetroCircleButtonStyle}" HorizontalAlignment="Left" Height="55" Margin="106,110,0,0" VerticalAlignment="Top" Width="55" Click="button_Click">   
  4. <Rectangle Width="20" Height="20" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">   
  5. <Rectangle.OpacityMask>   
  6. <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_disk}" />   
  7. </Rectangle.OpacityMask>   
  8. </Rectangle>   
  9. </Button>  
  10.         <Button ToolTip="Update an emplyee record" x:Name="button1" Style="{DynamicResource MetroCircleButtonStyle}" HorizontalAlignment="Left" Height="55" Margin="221,110,0,0" VerticalAlignment="Top" Width="55">   
  11. <Rectangle Width="20" Height="20" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">   
  12. <Rectangle.OpacityMask>   
  13. <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_edit_add}" />   
  14. </Rectangle.OpacityMask>   
  15. </Rectangle>   
  16. </Button> Button ToolTip="Delete an employee record" x:Name="button2" Style="{Dynamic Resource MetroCircleButtonStyle}" HorizontalAlignment="Left" Height="55" Margin="335,110,0,0" VerticalAlignment="Top" Width="55">  
  17.         <Rectangle Width="20" Height="20" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">  
  18.             <Rectangle.OpacityMask>  
  19.                 <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_delete}" />  
  20.             </Rectangle.OpacityMask>  
  21.         </Rectangle>  
  22.   
  23.         </Button>  
  24.     </Grid>  
  25. </Controls:MetroWindow>  
Complete App XAML source
  1. <Application x:Class="WpfApplication7.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication7" StartupUri="MainWindow.xaml">  
  2.     <Application.Resources>  
  3.         <ResourceDictionary>  
  4.             <ResourceDictionary.MergedDictionaries>  
  5.                 <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->  
  6.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />  
  7.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />  
  8.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />  
  9.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />  
  10.                 <!-- Accent and AppTheme setting -->  
  11.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Magenta.xaml" />  
  12.                 <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />  
  13.                 <ResourceDictionary Source="/Resources/Icons.xaml" />  
  14.             </ResourceDictionary.MergedDictionaries>  
  15.         </ResourceDictionary>  
  16.     </Application.Resources>  
  17. </Application>  
Step 6

Hit Ctrl+Shift+B to build the project. Then, hit F5 to check the output.

Output

output





Change the accent color and app theme to check the output.
  1. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Magenta.xaml" />   
  2. <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />   


Conclusion

I hope you liked this article. Please provide your valuable comments and suggestions.

Happy Coding!!!

Up Next
    Ebook Download
    View all
    Learn
    View all