c# - 在 Run 中指定相对 FontSize

标签 c# xaml

给定一个具有多次运行的 TextBlock,我如何指定相对 FontSize?例如,如果 TextBlock 的 FontSize 为 10,我希望其中一次 Run 为 15。但如果 FontSize 变为 20,我希望 Run 变为 30。

最佳答案

您没有说明这是针对 WPF 还是其他 XAML 框架。该示例适用于 WPF。

这通常是通过绑定(bind)和值转换器来完成的。

ValueConverter代码

class FontSizeConverter : IValueConverter {
  public object Convert(object value, Type targetType, object parameter, 
         System.Globalization.CultureInfo culture) {
    // add input parameter testing as needed.
    var originalFontSize = (double)value;
    double alteredFontSize = originalFontSize * Ratio; ;   

    return alteredFontSize;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
    throw new NotImplementedException();
  }

  // Create a ratio property 
  // allows the converter to be used for different font ratios
  public double Ratio { get; set; }
}

在 XAML 资源中实例化转换器并设置 Ratio 属性。

XAML

<Window.Resources>
    <local:FontSizeConverter x:Key='FontSizeConverter'
                             Ratio='1.5' />
 </Window.Resources>

然后使用相对绑定(bind)来获取父 TextBlock FontSize。将值父值传递给 ValueConverter。

XAML

<TextBlock FontSize='20'>
  <Run Text='The first bit.'
       FontSize='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
  <Run Text='The second bit' />          
  <Run Text='The third bit' />
</TextBlock>

如果要将绑定(bind)应用于 TextBlock 中的所有 Run,您可以为每个 Run 重复此绑定(bind),或创建使用该绑定(bind)的样式。

XAML

<Style TargetType='Run'>
      <Setter Property='FontSize'
              Value='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
    </Style>

完整的 XAML

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local='clr-namespace:SO_Questions'
        x:Class="SO_Questions.TextRunWindow"
        Title="TextRunWindow"
        Height="600"
        Width="600">
  <Window.Resources>
    <local:FontSizeConverter x:Key='FontSizeConverter'
                             Ratio='2.5' />
    <Style TargetType='Run'>
      <Setter Property='FontSize'
              Value='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' />
    </Style>
  </Window.Resources>
  <Grid>
<TextBlock FontSize='20'>
  <Run Text='The first bit.'
       FontSize='{Binding FontSize, Converter={StaticResource FontSizeConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType=TextBlock}}' /><Run Text=' ' />
<Run Text='The second bit' />            
<Run Text='The third bit' />
</TextBlock>
  </Grid>
</Window>

关于c# - 在 Run 中指定相对 FontSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27790525/

相关文章:

c# - 迁移到 .Net 4 : Null reference exceptions thrown when adding event in xaml

wpf - 当 IsReadOnly 为 true 时,RichTextBox 会忽略击键(Home、End、PgUp、PgDn)

c# - 数据绑定(bind)、多线程和单元测试

c# - WPF 某些图像在加载时旋转

c# - 客户端如何知道 wcf 服务正在运行?

c# - 通过事件通知实例

c# - 在 C# 中运行时使用外部 resx 文件(未嵌入)

c# - 为什么我需要 'dummy' 行代码才能加载PresentationFramework?

c# - 异步任务正在卡住 UI

c# - WP8 LongListSelector 中的自动高度图像