【问题标题】:How to extract font size of content如何提取内容的字体大小
【发布时间】:2012-03-13 08:51:42
【问题描述】:

我一直致力于使用 RichTextBox(MyRTB) 制作自己的小型文本编辑器。当使用此代码块更改值时,我制作了一个组合框来更改 RichTextBox 中选定文本的字体:

private void CmbFont_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (MyRTB != null)
        {                
            string fontsize = (((ComboBoxItem)CmbFont.SelectedItem).Content).ToString();
            MyRTB.Selection.ApplyPropertyValue(Run.FontSizeProperty, fontsize);
        }
    }

现在,我希望每次在 RichTextBox 中选择具有不同字体大小的文本字符串时,我的 Combobox 值都会发生变化。这可能吗?

谢谢

【问题讨论】:

    标签: c# silverlight richtextbox


    【解决方案1】:

    向选择更改事件添加事件处理程序。在该事件处理程序中,从 RichTextBox 选择中获取 TextElement.FontSizeProperty

    ...
    MyRTB.SelectionChanged += OnSelectionChanged;
    ...
    
    
    void OnSelectionChanged()
    {
     var fontSize = MyRTB.Selection.GetPropertyValue(TextElement.FontSizeProperty);
     if (fontSize == DependencyProperty.UnsetValue)
     {
      // Selection has text with different font sizes.
     }
     else {
      // (double)fontSize is the current font size. Update Cmb_Font.. 
     }
    }
    

    确保不要递归调用 OnSelectionChanged 和 CmbFont_SelectionChanged。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2014-04-15
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多