Shaked Dahan Shaked Dahan - 1 month ago 47
C# Question

Icon Not Appearing In MahApps RightWindowCommand

I started using mahapps a few days ago (still learning) and I tried to add an icon to the title bar, I did everything same as the example at their website and it won't appear. That's what I'm trying:

<Controls:MetroWindow.RightWindowCommands>
<Controls:WindowCommands>
<StackPanel Orientation="Horizontal">
<Rectangle Width="20"
Height="20"
Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}" />
</Rectangle.OpacityMask>
</Rectangle>
<Button Margin="4 0 0 0" Content="settings" Click="Button_Click"/>
</StackPanel>
</Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>`


And my app.xaml:

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>


How can I make the icons appear? Here is an example of what I want it to look like: What it should look like

Answer

Try this:

MainWindow.xaml:

<Controls:MetroWindow x:Class="MahApps.Metro.Application31.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  Icon="mahapps.metro.logo2.png"
                  BorderBrush="{StaticResource AccentColorBrush}"
                  BorderThickness="2"
                  Title="MainWindow"
                  Height="350"
                  Width="350">

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
        <Button>
            <StackPanel Orientation="Horizontal">
                <Rectangle Width="20"
                           Height="20"
                           Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                    <Rectangle.OpacityMask>
                        <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}" />
                    </Rectangle.OpacityMask>
                </Rectangle>
                <TextBlock Margin="5,0,0,0">Settings</TextBlock>
            </StackPanel>
        </Button>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>

<Grid>
    <Rectangle Fill="{StaticResource AccentColorBrush}">
        <Rectangle.OpacityMask>
            <VisualBrush Stretch="Fill"
                         Visual="{StaticResource appbar_cog}" />
        </Rectangle.OpacityMask>
    </Rectangle>
</Grid>

App.xaml:

<Application x:Class="MahApps.Metro.Application31.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro.Resources;component/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

enter image description here


Notes: for everything to work correctly you need to:

1) explicitly add mahapps.metro.logo2.png to your project; 2) explicitly add a project reference to MahApps.Metro.Resources

enter image description here