我会跳到追逐:有没有办法告诉WPF TextBlock自己测量它的大小在它改变时不会改变FontWeight?
我有一个TextBlock根据样式动态更改字体权重.该TextBlock是内部的RadioButton所以检查时,它是勇敢的,否则正常:
<Style x:Key="BoldWhenChecked" TargetType="RadioButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="TextElement.FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style
Run Code Online (Sandbox Code Playgroud)
以下是单选按钮本身:
<StackPanel Orientation="Horizontal">
<RadioButton Style="{StaticResource BoldWhenChecked}">
<TextBlock Text="Item 1" />
</RadioButton>
<RadioButton Style="{StaticResource BoldWhenChecked}">
<TextBlock Text="Item 2" />
</RadioButton>
<RadioButton Style="{StaticResource BoldWhenChecked}">
<TextBlock Text="Item 3" />
</RadioButton>
etc...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于我没有使用固定宽度的字体,TextBlock当字体重量改变时,改变的宽度,并且整个单选按钮面板相应地移动,这在视觉上是不和谐的.
我为此创建了一个解决方法,方法是TextBlock在 的内容中添加一个 hiddenRadioButton并将其FontStyle显式设置为Bold:
<RadioButton Style="{StaticResource BoldWhenChecked}">
<Grid>
<TextBlock Text="Item 1" />
<TextBlock Text="Item 1" FontStyle="Bold" Visibility="Hidden" />
</Grid>
</RadioButton>
Run Code Online (Sandbox Code Playgroud)
这样,当 RadioButton 被选中并且可见TextBlock被加粗时,宽度不会改变,因为隐藏TextBlock已经适当地调整了网格的大小。
| 归档时间: |
|
| 查看次数: |
1605 次 |
| 最近记录: |