简体   繁体   中英

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

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:

只有标记应该变大

somoene an Idea?

sry for my english Im not good in English grammar

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM