wpf 使用 MahApps.Metro 时,应用程序图标会拉伸到标题栏高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21284470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:34:21  来源:igfitidea点击:

Application icon stretches to title bar height when using MahApps.Metro

wpficonsmahapps.metro

提问by avantprime

How to prevent application icon from stretching to the height of the title bar when using MahApps.Metro? No spaces between icon and title bar edges no matter what size of icon used. I have also tried using multi-sized icons and this does not work.

使用 MahApps.Metro 时如何防止应用程序图标拉伸到标题栏的高度?无论使用什么大小的图标,图标和标题栏边缘之间都没有空格。我也尝试过使用多尺寸图标,但这不起作用。

Here is an example out of the box of what it looks like:

这是一个开箱即用的示例:

MahApps.Metro Icon

MahApps.Metro 图​​标

回答by MuiBienCarlota

Strongly inspired from mahapps punker76's code, you can do this:

受到mahapps punker76 代码的强烈启发,您可以这样做:

<MahApps:MetroWindow.IconTemplate>
    <DataTemplate>
        <Grid Width="{TemplateBinding Width}"
                 Height="{TemplateBinding Height}"
                 Margin="4"
                 Background="Transparent"
                 RenderOptions.EdgeMode="Aliased"
                 RenderOptions.BitmapScalingMode="HighQuality">
            <Image Source="Images/Document Alignment.ico"></Image>
        </Grid>
    </DataTemplate>
</MahApps:MetroWindow.IconTemplate>

But a Icon Margin property could be simpler.

但是 Icon Margin 属性可能更简单。

回答by Viv

You got a couple options to achieve your requirement.

您有几个选项可以满足您的要求。

  • Tweak the library to add a Marginproperty to Iconand submit a pull request
  • 调整库以添加Margin属性Icon并提交拉取请求

MahApps.Metrois on Git, you could just fork it and tweak the Title bar icon with a Marginproperty as you desire.

MahApps.Metro在 Git 上,您可以将它分叉并根据需要调整带有Margin属性的标题栏图标。

Currently TitleBar Icon does not seem to have this property and starts from the edges based on it's xaml definition.

目前 TitleBar Icon 似乎没有这个属性,并且根据它的 xaml 定义从边缘开始。

<Grid x:Name="PART_TitleBar" Background="Transparent"
      Height="{Binding TitlebarHeight, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"
      Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}"
      Grid.Column="0">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Image Visibility="{TemplateBinding ShowIconOnTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}"
           Source="{TemplateBinding Icon}"
           RenderOptions.EdgeMode="Aliased"
           RenderOptions.BitmapScalingMode="HighQuality" />

You could then submit a pull request to allow the authors to integrate it into the main library if they reckon it's a nice feature.

如果作者认为这是一个不错的功能,那么您可以提交拉取请求以允许他们将其集成到主库中。

  • Easier option: Tweak your Title bar Icon image with a transparent padding
  • 更简单的选项:使用透明填充调整标题栏图标图像

In the source for your Title bar image add a transparent padding. Something like:

在标题栏图像的源中添加透明填充。就像是:

enter image description here

在此处输入图片说明

Now when you use this as the Iconin your MetroWindowyou should have an output like:

现在,当您将其用作Iconin 时,您MetroWindow应该有如下输出:

enter image description here

在此处输入图片说明