Change font size

Eduardo Gomez 4,136 Reputation points
2021-06-12T13:29:55.637+00:00

Hello everyone, I have a little bug in my WPF application, that is bugging me. I am changing the font size of my text in my richtextbox, the weird thing is, that sometimes it works, and sometimes it doesn't,

It's very weird, and I was thinking that someone, would have an idea why?

This is the part of the code that controls that

private void NoteContent_SelectionChanged(object sender, RoutedEventArgs e) {

        var selectionFontWeight = NoteContent.Selection.GetPropertyValue(TextElement.FontWeightProperty);  
        boldBton.IsChecked = (selectionFontWeight != DependencyProperty.UnsetValue)  
            && selectionFontWeight.Equals(FontWeights.Bold);  

        var selectionFontStyle = NoteContent.Selection.GetPropertyValue(TextElement.FontStyleProperty);  
        italicBton.IsChecked = (selectionFontStyle != DependencyProperty.UnsetValue)  
            && selectionFontStyle.Equals(FontStyles.Italic);  

        var selectionFontDecoration = NoteContent.Selection.GetPropertyValue(Inline.TextDecorationsProperty);  
        UndelineBton.IsChecked = (selectionFontDecoration != DependencyProperty.UnsetValue)  
            && selectionFontDecoration.Equals(TextDecorations.Underline);  

        FontsComboBox.SelectedItem = NoteContent.Selection.GetPropertyValue(FontFamilyProperty);  
        SizeConboBox.Text = (NoteContent.Selection.GetPropertyValue(FontSizeProperty)).ToString();  
    }  

    private void FontsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {  
        if (FontsComboBox.SelectedItem != null) {  
            NoteContent.Selection.ApplyPropertyValue(FontFamilyProperty, FontsComboBox.SelectedItem);  
        }  
    }  

    private void SizeConboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {  
        NoteContent.Selection.ApplyPropertyValue(FontSizeProperty, SizeConboBox.Text);  
    }  

Another thing to mention is that if I change my font family to "Arial", it will work fine but the rest no, or sometimes I have to change it twice in order to see the effect

105092-font-size-changed.jpg

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. DaisyTian-1203 11,651 Reputation points Moderator
    2021-06-14T06:56:00.187+00:00

    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:
    105301-3.gif


    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hrvoje Voda 1 Reputation point
    2021-09-01T12:30:24.573+00:00

    This works great!

    I was wondering how to do the same thing with Fonts.
    I have list of Fonts in combobox and what to change selected text in richtextbox with selected font in combobox?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.