【发布时间】:2017-08-03 08:04:31
【问题描述】:
我在理解 WPF 中的不透明度时遇到了问题。我在下面发布了代码。我的问题是:
- 为什么矩形和字体的颜色不同?
- 当我更改字体大小时,为什么两个 TextBlock 的颜色不同?
我希望,当我使用颜色选择器窥探颜色时,不透明度为 50% 的黑色会呈现 #7F7F7F,但我会为较小的 TextBlock 获得 #C2C2C2,而对于较大的字体,我会获得预期的 #7F7F7F和矩形。
问题已在https://github.com/ButchersBoy/MaterialDesignInXamlToolkit/issues/408 部分提出,但未得到正确回答。
感谢任何帮助!
代码是:
<Window x:Class="WpfPlay.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:WpfPlay"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="800" Background="White">
<Window.Resources>
<SolidColorBrush x:Key="ForeBrush" Color="Black" Opacity="0.5"/>
<SolidColorBrush x:Key="BackBrush" Color="White" Opacity="1.0"/>
<Style TargetType="TextBlock" x:Key="TextBlockStyle">
<Setter Property="Foreground" Value="{StaticResource ForeBrush}"/>
<Setter Property="Background" Value="{StaticResource BackBrush}"/>
<Setter Property="FontSize" Value="48"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style TargetType="TextBlock" x:Key="TextBlockLargeStyle">
<Setter Property="Foreground" Value="{StaticResource ForeBrush}"/>
<Setter Property="Background" Value="{StaticResource BackBrush}"/>
<Setter Property="FontSize" Value="100"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Window.Resources>
<StackPanel Orientation="Vertical" Background="White">
<TextBlock Text="Click me" HorizontalAlignment="Center" Style="{StaticResource TextBlockStyle}" Margin="20"/>
<TextBlock Text="Click me" HorizontalAlignment="Center" Style="{StaticResource TextBlockLargeStyle}" Margin="20"/>
<Rectangle Width="100" Height="100" HorizontalAlignment="Center" Margin="20" Fill="{StaticResource ForeBrush}"/>
</StackPanel>
</Window>
【问题讨论】:
-
我不知道为什么会这样,但是我可以在我的电脑上验证 FontSize = 80 的颜色变化。
-
根据support.microsoft.com/nl-be/help/2712383/…,Net 3.5 在 fonsize 为 100 或更大时遇到困难(特别是在转换后 100pts、dpi 设置等时)。也许还有一些关于不透明度和 100pts 及更大字体大小的“遗留”错误。
标签: wpf colors rendering opacity