如何提取内容的字体大小

时间:2012-02-29 22:58:34

标签: c# silverlight richtextbox

我一直在使用RichTextBox(MyRTB)制作我自己的小文本编辑器。当使用此代码块更改值时,我已使用Combobox更改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值都会更改。这可能吗?

由于

1 个答案:

答案 0 :(得分: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递归。