Austin Henley
Austin Henley

Reputation: 4633

Setting window icon with a static resource

I am trying to set the window icon in a WPF application that uses MahApps.Metro. I don't know how to set a static resource that I defined after the window's XAML to be the window's icon.

Below is the XAML, the resource I want is in Icons.xaml:

<controls:MetroWindow x:Class="Test1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="MainWindow" Height="350" Width="525" WindowState="Maximized">

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.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="/Resources/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Here is an example of how I use the resource on the window:

<Rectangle>
    <Rectangle.Fill>
        <VisualBrush Visual="{StaticResource appbar_alien}" />
    </Rectangle.Fill>
</Rectangle>

How do I set appbar_alien as the window's icon?

Upvotes: 2

Views: 8747

Answers (1)

MuiBienCarlota
MuiBienCarlota

Reputation: 2425

I've just answered to a question about icon too big in MahApps MetroWindow (Application icon stretches to title bar height when using MahApps.Metro) and same technique could give you a solution.

Basically, you have to set MetroWindow.IconTemplate property:

<MahApps:MetroWindow.IconTemplate>
    <DataTemplate>
        <Grid Width="{TemplateBinding Width}"
                 Height="{TemplateBinding Height}"
                 Margin="4"
                 Background="Transparent"
                 RenderOptions.EdgeMode="Aliased"
                 RenderOptions.BitmapScalingMode="HighQuality">
                 <Rectangle>
                     <Rectangle.Fill>
                         <VisualBrush Visual="{StaticResource appbar_alien}" />
                     </Rectangle.Fill>
                 </Rectangle>
        </Grid>
    </DataTemplate>
</MahApps:MetroWindow.IconTemplate>

Depending of your "/Resources/Icons.xaml", you may have to stretch/scale it.

Upvotes: 5

Related Questions