使用静态资源设置窗口图标

时间:2013-07-07 00:42:15

标签: wpf xaml mahapps.metro

我正在尝试在使用MahApps.Metro的WPF应用程序中设置窗口图标。我不知道如何在窗口的XAML之后将我定义的静态资源设置为窗口的图标。

以下是XAML,我想要的资源位于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>

以下是我如何使用 on 窗口的示例:

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

如何将appbar_alien设置为窗口的图标?

1 个答案:

答案 0 :(得分:5)

我刚刚在MahApps MetroWindow(Application icon stretches to title bar height when using MahApps.Metro)中回答了关于图标太大的问题,同样的技术可以为您提供解决方案。

基本上,您必须设置MetroWindow.IconTemplate属性:

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

根据您的&#34; /Resources / Icon.xaml&#34;,您可能需要拉伸/缩放它。