如何提取内容的字体大小
本文关键字:字体 何提取 提取 | 更新日期: 2023-09-27 18:26:27
我一直在使用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中选择一个字体大小不同的文本字符串时,我的组合框值都会发生变化。这可能吗?
感谢
将事件处理程序添加到选择更改的事件中。在该事件处理程序中,从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&CmdFontSelectionChanged递归。