在工程中用到MahApps.Metro开源库,将软件进行多语言化的过程中发现一个问题,就是WindowButtonCommands的ToolTip不便于多语言化,
将 MahApps.Metro/src/MahApps.Metro/Themes/WindowButtonCommands.xaml 文件对应的Style放在自己本地字典里,然后对需要多语言化的字段进行多语言化即可。当然,如果大家还有更好的实现方式,欢迎留言!
<ControlTemplate x:Key="MahApps.Templates.WindowButtonCommands" TargetType="{x:Type Controls:WindowButtonCommands}">
<StackPanel Orientation="Horizontal">
<Button x:Name="PART_Min"
Command="{x:Static SystemCommands.MinimizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMinButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}"
ToolTip="{Binding Minimize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="MIN">
<Binding Mode="OneWay"
Path="ShowMinButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Path Data="F1M0,6L0,9 9,9 9,6 0,6z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
SnapsToDevicePixels="True" />
</Button>
<Button x:Name="PART_Max"
Command="{x:Static SystemCommands.MaximizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMaxRestoreButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}"
ToolTip="{Binding Maximize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="MAX">
<Binding Mode="OneWay"
Path="ShowMaxRestoreButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<!-- normal state -->
<Path x:Name="PART_MaxPath"
Width="10"
Height="10"
Data="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
SnapsToDevicePixels="True" />
</Button>
<Button x:Name="PART_Close"
Command="{x:Static SystemCommands.CloseWindowCommand}"
Focusable="False"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabled, Mode=OneWay}"
RenderOptions.EdgeMode="Aliased"
ToolTip="{Binding Close, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="CLOSE">
<Binding Mode="OneWay"
Path="ShowCloseButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Line SnapsToDevicePixels="True"
Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
StrokeThickness="2.5"
X1="1"
X2="9"
Y1="1"
Y2="9" />
<Line SnapsToDevicePixels="True"
Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
StrokeThickness="2.5"
X1="1"
X2="9"
Y1="9"
Y2="1" />
</Grid>
</Button>
</StackPanel>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabled}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsAnyDialogOpen}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabledWithDialog}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="PART_Close" Property="IsEnabled" Value="False" />
</MultiDataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=WindowState}" Value="Maximized">
<Setter TargetName="PART_Max" Property="Command" Value="{x:Static SystemCommands.RestoreWindowCommand}" />
<Setter TargetName="PART_Max" Property="ToolTip" Value="{Binding Restore, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
<Setter TargetName="PART_MaxPath" Property="Data" Value="F1M0,10L0,3 3,3 3,0 10,0 10,2 4,2 4,3 7,3 7,6 6,6 6,5 1,5 1,10z M1,10L7,10 7,7 10,7 10,2 9,2 9,6 6,6 6,9 1,9z" />
</DataTrigger>
<Trigger Property="Theme" Value="Light">
<Setter TargetName="PART_Close" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightCloseButtonStyle}" />
<Setter TargetName="PART_Max" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMaxButtonStyle}" />
<Setter TargetName="PART_Min" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMinButtonStyle}" />
</Trigger>
<Trigger Property="Theme" Value="Dark">
<Setter TargetName="PART_Close" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkCloseButtonStyle}" />
<Setter TargetName="PART_Max" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMaxButtonStyle}" />
<Setter TargetName="PART_Min" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMinButtonStyle}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<ControlTemplate x:Key="MahApps.Templates.WindowButtonCommands.Win10" TargetType="{x:Type Controls:WindowButtonCommands}">
<StackPanel Orientation="Horizontal">
<Button x:Name="PART_Min"
Width="46"
Command="{x:Static SystemCommands.MinimizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMinButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}"
ToolTip="{Binding Minimize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="MIN">
<Binding Mode="OneWay"
Path="ShowMinButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Path Width="10"
Height="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0L10,0 10,1 10,1 1,1 0,1z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Stretch="Uniform" />
</Button>
<Button x:Name="PART_Max"
Width="46"
Command="{x:Static SystemCommands.MaximizeWindowCommand}"
Focusable="False"
IsEnabled="{Binding IsMaxRestoreButtonEnabled, RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}}"
ToolTip="{Binding Maximize, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="MAX">
<Binding Mode="OneWay"
Path="ShowMaxRestoreButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="ResizeMode"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<!-- normal state -->
<Path x:Name="PART_MaxPath"
Width="10"
Height="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M1,1L1,11 11,11 11,1z M0,0L12,0 12,12 0,12z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Stretch="Uniform" />
</Button>
<Button x:Name="PART_Close"
Width="46"
Command="{x:Static SystemCommands.CloseWindowCommand}"
Focusable="False"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabled, Mode=OneWay}"
ToolTip="{Binding Close, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
UseLayoutRounding="True">
<Button.Visibility>
<MultiBinding Converter="{x:Static Converters:ResizeModeMinMaxButtonVisibilityConverter.Instance}" ConverterParameter="CLOSE">
<Binding Mode="OneWay"
Path="ShowCloseButton"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
<Binding Mode="OneWay"
Path="UseNoneWindowStyle"
RelativeSource="{RelativeSource AncestorType={x:Type Controls:MetroWindow}}" />
</MultiBinding>
</Button.Visibility>
<Path Width="10"
Height="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="F1M8.583,8L13,12.424 12.424,13 8,8.583 3.576,13 3,12.424 7.417,8 3,3.576 3.576,3 8,7.417 12.424,3 13,3.576z"
Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
RenderOptions.EdgeMode="Aliased"
SnapsToDevicePixels="True"
Stretch="Uniform" />
</Button>
</StackPanel>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabled}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsAnyDialogOpen}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=IsCloseButtonEnabledWithDialog}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="PART_Close" Property="IsEnabled" Value="False" />
</MultiDataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:MetroWindow}}, Path=WindowState}" Value="Maximized">
<Setter TargetName="PART_Max" Property="Command" Value="{x:Static SystemCommands.RestoreWindowCommand}" />
<Setter TargetName="PART_Max" Property="ToolTip" Value="{Binding Restore, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}" />
<Setter TargetName="PART_MaxPath" Property="Data" Value="M1,4.56L1,14.56 11,14.56 11,4.56z M4,1L4,3.56 12,3.56 12,11 14,11 14,1z M3,0L15,0 15,12 12,12 12,15.56 0,15.56 0,3.56 3,3.56z" />
</DataTrigger>
<Trigger Property="Theme" Value="Light">
<Setter TargetName="PART_Close" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightCloseButtonStyle}" />
<Setter TargetName="PART_Max" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMaxButtonStyle}" />
<Setter TargetName="PART_Min" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LightMinButtonStyle}" />
</Trigger>
<Trigger Property="Theme" Value="Dark">
<Setter TargetName="PART_Close" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkCloseButtonStyle}" />
<Setter TargetName="PART_Max" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMaxButtonStyle}" />
<Setter TargetName="PART_Min" Property="Style" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DarkMinButtonStyle}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="MahApps.Styles.WindowButtonCommands.Base" TargetType="{x:Type Controls:WindowButtonCommands}">
<Setter Property="Background" Value="{DynamicResource MahApps.Brushes.TransparentWhite}" />
<Setter Property="DarkCloseButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Dark}" />
<Setter Property="DarkMaxButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Dark}" />
<Setter Property="DarkMinButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Dark}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.Black}" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="LightCloseButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Light}" />
<Setter Property="LightMaxButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Light}" />
<Setter Property="LightMinButtonStyle" Value="{DynamicResource MahApps.Styles.Button.MetroWindow.Light}" />
<Setter Property="Template" Value="{StaticResource MahApps.Templates.WindowButtonCommands}" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<Style x:Key="MahApps.Styles.WindowButtonCommands"
BasedOn="{StaticResource MahApps.Styles.WindowButtonCommands.Base}"
TargetType="{x:Type Controls:WindowButtonCommands}">
<Style.Triggers>
<DataTrigger Binding="{Binding ParentWindow.ShowTitleBar, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="Foreground" Value="{DynamicResource MahApps.Brushes.IdealForeground}" />
</DataTrigger>
</Style.Triggers>
</Style>