Home > Software engineering >  How can I increase the FontSize of a text with an ComboBox, but just the selected part of the Text?
How can I increase the FontSize of a text with an ComboBox, but just the selected part of the Text?

Time:06-20

So Ive got an RichTextbox in which I write Text, that Text I want to increase with the Selected FontSize from an ComboBox. So Far Ive got this:

viewModel:

  public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<int> FontSizes { get; set; }

    private int _selectedFont;

    public int SelectedFont
    {
        get { return _selectedFont; }
        set
        {
            _selectedFont = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedFont)));
        }
    }

    public ViewModel()
    {
        FontSizes = new ObservableCollection<int>() { 10, 15, 20 };
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

MainWindow:

 <ToolBar Background="#0E8F88" Style="{DynamicResource ToolBarStyle}"
                         Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right">
                    <ComboBox  x:Name="fontsCombo" ItemsSource="{Binding FontSizes}"
          SelectedItem="{Binding SelectedFont, UpdateSourceTrigger=PropertyChanged}" >
                       
                    </ComboBox>
                </ToolBar>


 <RichTextBox Tag="Überschrift" FontSize="{Binding SelectedFont, UpdateSourceTrigger=PropertyChanged}" x:Name="tb_1" FontWeight="{Binding bold1}" 
                          FontStyle="{Binding italicCode2}" AcceptsTab="True"
                           
                          Grid.Row="1" Visibility="{Binding disableGray, UpdateSourceTrigger=PropertyChanged}" 
                         HorizontalAlignment="Center" Margin="10 0 10 0" LostFocus="tb_1_LostFocus">

                    </RichTextBox>

And it Increase the text but he does Increase all of the Text, but I want only the marked Text get bigger:

Only Marked should get Bigger

somoene an Idea?

sry for my english Im not good in English grammar

CodePudding user response:

Applying the font size/family to the selected text in RichTextBox is not straightforwardly applicable using MVVM. You shall create a custom control inheriting from RichTextBox. You can use the answer at this SO Question and then when font size is changed (instead of OnTextInput event), create a Run from selected text and update its FontSize.

  • Related