更改 TextBlock 的字体系列时缺少关闭、最大化和最小化图标
Missing Closing, Maximize and Minimize icons when changing Font Family of TextBlock
我正在创建一个 WPF 应用程序,它使用 ModernWpf 来设置所有内容的样式。我还在 window:
上使用了这两个参数
ui:ThemeManager.RequestedTheme="Dark"
ui:WindowHelper.UseModernWindowStyle="True"
它工作正常,window 栏现在变暗了。但是只要我想像那样为每个 TextBlock 设置字体系列
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
</Style>
它不断更改 ModernWpf 的 FontFamily Window,但字体不包含所需的图标。我是否必须设置一个键并将每个 TextBlock 样式手动设置为该键,还是有其他方法?或者是否可以设置 window 图标的字体系列?
这就是 _ [ ] X 按钮现在的样子:
在 App.xaml
中定义明确的 TextBlock
样式可能不是一个好主意,因为它会影响应用程序中的 all TextBlock
元素.
无论如何,如果您为 TitleBarButton
定义自定义 ControlTemplate
,将 ContentPresenter
替换为 TextBlock
,您仍然可以使标题按钮按预期显示:
<Style x:Key="CustomStyle" TargetType="ui:TitleBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ui:TitleBarButton">
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<TextBlock
x:Name="Content"
Text="{TemplateBinding Content}"
FontFamily="Segoe MDL2 Assets"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsActive" Value="False">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InactiveBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InactiveForeground}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverForeground}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PressedBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PressedForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="RootGrid" Value="{DynamicResource SystemControlDisabledTransparentBrush}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您需要将所有 windows 的 TitleBar.ButtonStyle
附加 属性 设置为您的自定义样式,就像您设置 WindowHelper.UseModernWindowStyle
:
<Window ... ui:TitleBar.ButtonStyle="{StaticResource CustomStyle}" />
我正在创建一个 WPF 应用程序,它使用 ModernWpf 来设置所有内容的样式。我还在 window:
上使用了这两个参数ui:ThemeManager.RequestedTheme="Dark"
ui:WindowHelper.UseModernWindowStyle="True"
它工作正常,window 栏现在变暗了。但是只要我想像那样为每个 TextBlock 设置字体系列
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
</Style>
它不断更改 ModernWpf 的 FontFamily Window,但字体不包含所需的图标。我是否必须设置一个键并将每个 TextBlock 样式手动设置为该键,还是有其他方法?或者是否可以设置 window 图标的字体系列?
这就是 _ [ ] X 按钮现在的样子:
在 App.xaml
中定义明确的 TextBlock
样式可能不是一个好主意,因为它会影响应用程序中的 all TextBlock
元素.
无论如何,如果您为 TitleBarButton
定义自定义 ControlTemplate
,将 ContentPresenter
替换为 TextBlock
,您仍然可以使标题按钮按预期显示:
<Style x:Key="CustomStyle" TargetType="ui:TitleBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ui:TitleBarButton">
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<TextBlock
x:Name="Content"
Text="{TemplateBinding Content}"
FontFamily="Segoe MDL2 Assets"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsActive" Value="False">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InactiveBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InactiveForeground}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverForeground}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="RootGrid" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PressedBackground}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PressedForeground}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="RootGrid" Value="{DynamicResource SystemControlDisabledTransparentBrush}" />
<Setter Property="TextElement.Foreground" TargetName="Content" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您需要将所有 windows 的 TitleBar.ButtonStyle
附加 属性 设置为您的自定义样式,就像您设置 WindowHelper.UseModernWindowStyle
:
<Window ... ui:TitleBar.ButtonStyle="{StaticResource CustomStyle}" />