首页 > 解决方案 > 具有相同颜色(不透明度)和不同字体大小的两个文本块出现不同

问题描述

我有两个具有相同前景的 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>

标签: wpfwpf-controlsopacitytextblocktext-coloring

解决方案


我在使用 WPF 应用程序时遇到了同样的问题。我不知道为什么OpacityonSolidColorBrush不起作用,但解决方案是在 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>

推荐阅读