wpf - 具有相同颜色(不透明度)和不同字体大小的两个文本块出现不同
问题描述
我有两个具有相同前景的 TextBlock,但显示方式不同。TextBlocks 之间的差异是 FontSize。在放大/缩小时,颜色也会发生变化。
通过设置<Setter Property="TextOptions.TextFormattingMode" Value="Display" />
它可以工作,但是 ViewBox 中显示的文本会变得模糊。TextFormattingMode "Display" 不能在 ViewBox 中使用,请参见此处。
为什么会这样?为什么默认颜色不同?有什么解决办法吗?
<Window x:Class="ColoringWithOpacity.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ColoringWithOpacity"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Background="Black">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontWeight" Value="Semibold"/>
</Style>
<Style TargetType="{x:Type Run}">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontWeight" Value="Semibold"/>
</Style>
<SolidColorBrush x:Key="W25" Opacity="0.25" Color="#FFFFFF"/>
</Window.Resources>
<UniformGrid Columns="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Viewbox Grid.RowSpan="2">
<TextBlock Foreground="{StaticResource W25}" Text="13.55" >
<!--<Run Text="13.55"/>--> <!--Works the same-->
</TextBlock>
</Viewbox>
<Viewbox Grid.Row="2" Grid.Column="2">
<TextBlock Foreground="{StaticResource W25}" Text="mm" >
<!--<Run Text="mm"/>--> <!--Works the same-->
</TextBlock>
</Viewbox>
</Grid>
<Viewbox>
<TextBlock Foreground="{StaticResource W25}">
<Run Text="13.55" FontSize="64"/>
<Run Text="mm" FontSize="40"/>
</TextBlock>
</Viewbox>
解决方案
我在使用 WPF 应用程序时遇到了同样的问题。我不知道为什么Opacity
onSolidColorBrush
不起作用,但解决方案是在 UI 元素上移动不透明度,而不是SolidColorBrush
.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d" Background="Black"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontWeight" Value="Semibold"/>
<Setter Property="Opacity" Value="0.25"/>
</Style>
<Style TargetType="{x:Type Run}">
<Setter Property="FontFamily" Value="Segoe UI"/>
<Setter Property="FontWeight" Value="Semibold"/>
</Style>
<SolidColorBrush x:Key="W25" Color="#FFFFFF" />
</Window.Resources>
<UniformGrid Columns="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Viewbox Grid.RowSpan="2">
<TextBlock Foreground="{StaticResource W25}" Text="13.55" >
<!--<Run Text="13.55"/>--> <!--Works the same-->
</TextBlock>
</Viewbox>
<Viewbox Grid.Row="2" Grid.Column="2">
<TextBlock Foreground="{StaticResource W25}" Text="mm" >
<!--<Run Text="mm"/>--> <!--Works the same-->
</TextBlock>
</Viewbox>
</Grid>
<Viewbox>
<TextBlock Foreground="{StaticResource W25}">
<Run Text="13.55" FontSize="64"/>
<Run Text="mm" FontSize="40"/>
</TextBlock>
</Viewbox>
</UniformGrid>
</Window>
推荐阅读
- python - 使用复合索引从 DataFrame 中提取值的正确方法?
- java - 无法传输工件传输失败
- r - 使用多个条件查找(或合并)2 个数据帧
- c# - 将 G Suite 驱动器文件和文件夹从一个域复制到另一个域
- python - 在集合中查找内容与在 python 中查找列表的速度
- node.js - 如何使用firebase函数刷新google oauth2上的令牌?
- amazon-web-services - 将 Schedule-ScheduledJob 注册为 LocalSystem 用户
- typescript - 尽管存在,但编译器“未找到”类型,而 IntelliSense 找到它们?
- python - Python For 循环输出列模式
- mongodb - MongoDB 查询索引扫描未按预期过滤