我正在尝试使用MahApps.Metro 0.13.1.0在窗口标题栏中添加"刷新"按钮.
我的App.xaml是这样的:
<Application x:Class="VersionSwitcher.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Views/MainWindow.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<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="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:VersionSwitcher.ViewModel" />
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
然后在我的MainWindow.xaml中:
<Controls:MetroWindow.WindowCommands>
<Controls:WindowCommands>
<Button ToolTip="{x:Static p:Resources.Button_Refresh}"
Style="{DynamicResource MetroCircleButtonStyle}" Width="30" Height="30">
<Rectangle Fill="Black">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill"
Visual="{DynamicResource appbar_refresh}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
</Controls:WindowCommands>
</Controls:MetroWindow.WindowCommands>
Run Code Online (Sandbox Code Playgroud)
我在标题栏中有一个灰色圆圈,但没有图标!任何图标(来自Resources/Icons.xaml)都做同样的事情.
如果图标放在窗口内容而不是标题栏中,它会做同样的事情.
我错过了什么?
谢谢 !
我发现为Rectangle设置宽度和高度会显示图标.如何随时填充按钮我该怎么办?
你看不出来就算是Rectangle有它的大小为0,ActualWidth并且ActualHeight您可以通过看史努比
如果您对评论中提到的WPF了解不多,请熟悉Snoop.伟大的工具,可以节省你很多头部抓挠的时刻:)

所以在简单的英语中,我们添加一个Rectangle作为Contenta的Button.现在按钮有一些尺寸.然而,Rectangle不会,因此最终得到0.如果是某些文本,它将获得隐式大小.
因此,要对其进行排序,请为Rectangle指定合适的宽度/高度.
如果希望Rectangle为Button的大小,可以使用RelativeSource Binding作为Width和Height从按钮获取它.但是在你的情况下它是一个Styled按钮,它已经使用了一个椭圆,所以它需要一些填充.
来自github的MahApps.Metro:
显示他们建议的方法:(显式大小小于父按钮的内容)
<Button Width="50"
Height="50"
Margin="0, 10, 0, 0"
Style="{DynamicResource MetroCircleButtonStyle}">
<Rectangle Width="20"
Height="20"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill"
Visual="{DynamicResource appbar_city}" />
</Rectangle.OpacityMask>
</Rectangle>
</Button>
Run Code Online (Sandbox Code Playgroud)