Please try below code to change your FontSize:
Xaml code is:
<RichTextBox Name="rtf" Width="200" Height="200" SelectionChanged="rtf_SelectionChanged" >
<FlowDocument>
<Paragraph>
<Run>Paragraph 1</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 2</Run>
</Paragraph>
<Paragraph>
<Run>Paragraph 3</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
<WrapPanel VerticalAlignment="Center" Width="100">
<ComboBox x:Name="FontSizeCombo" Height="23" Width="40" Margin="5,2,5,2" IsEditable="True" SelectionChanged="OnFontSizeComboSelectionChanged">
</ComboBox>
</WrapPanel>
C# code is:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
FontSizeCombo.Items.Add("10");
FontSizeCombo.Items.Add("12");
FontSizeCombo.Items.Add("14");
FontSizeCombo.Items.Add("18");
FontSizeCombo.Items.Add("24");
FontSizeCombo.Items.Add("36");
}
private void OnFontSizeComboSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (FontSizeCombo.SelectedItem == null) return;
if (FontSizeCombo.SelectedItem.ToString() == "{DependencyProperty.UnsetValue}")
{
FontSizeCombo.SelectedItem = null;
return;
}
var pointSize = FontSizeCombo.SelectedItem.ToString();
var pixelSize = Convert.ToDouble(pointSize) * (96 / 72);
var textRange = new TextRange(rtf.Selection.Start, rtf.Selection.End);
textRange.ApplyPropertyValue(TextElement.FontSizeProperty, pixelSize);
}
private void rtf_SelectionChanged(object sender, RoutedEventArgs e)
{
var textRange = new TextRange(rtf.Selection.Start, rtf.Selection.End);
var fontSize = textRange.GetPropertyValue(TextElement.FontSizeProperty);
FontSizeCombo.Text = fontSize.ToString();
}
}
The result picture is:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.