Paragraph mark of UWP RichEditBox is not resized

前提是你 提交于 2020-06-29 04:02:32

问题


With this code (Editor.Document.Selection.CharacterFormat.Size = 20;) I change the font size of a UWP RichEditBox in the Page_Loaded-handle. When I start typing some characters, everything works fine. But when I select the paragraph mark and then type something, this text appears in the wrong font size (10.5).

What I've tried is to expand the selection before setting the font size, but it seems that there is no paragraph mark when there's no text. But when the rich edit box is empty and I press Shift+Right Arrow ⟶ (as I would normally select the paragraph mark), the font size is set back to 10.5.

Is there any workaround to prevent that the font size is set back to 10.5 in any case?

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBlock x:Name="FontSizeBlock" Grid.Row="0"></TextBlock>
    <RichEditBox x:Name="Editor" Grid.Row="1" SelectionChanged="HandleRichEditBox_SelectionChanged"></RichEditBox>
</Grid>


public sealed partial class MainPage : Page {
    public MainPage() {
        this.InitializeComponent();
        this.Loaded += Page_Loaded;
    }

    private void Page_Loaded(object sender, RoutedEventArgs e) {
        Editor.Document.Selection.SetRange(0, 1);
        Editor.Document.Selection.CharacterFormat.Size = 20;
    }

    private void HandleRichEditBox_SelectionChanged(object sender, RoutedEventArgs e) {
        FontSizeBlock.Text = "FontSize: " + Editor.Document.Selection.CharacterFormat.Size;
    }
}


回答1:


Is there any workaround to prevent that the font size is set back to 10.5 in any case?

For your requirement, you could force font size to 20 when you press Shift+Right. Please refer the following code.

private void HandleRichEditBox_SelectionChanged(object sender, RoutedEventArgs e)
{
    FontSizeBlock.Text = "FontSize: " + Editor.Document.Selection.CharacterFormat.Size;
    if (Editor.Document.Selection.CharacterFormat.Size == 10.5)
    {
        Editor.Document.Selection.SetRange(0, 1);
        Editor.Document.Selection.CharacterFormat.Size = 20;
    }
}



回答2:


When further investigating my problem, I noted that my issue doesn't occur when there has already been some text in the RichEditBox which was deleted.

Therefore, I have tried to programmatically append, select and remove characters in the Page_Loaded-handle, and this approach worked for me.

private void Page_Loaded(object sender, RoutedEventArgs e) {
    // set any character
    Editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, "a");
    Editor.Document.Selection.Expand(Windows.UI.Text.TextRangeUnit.Paragraph);
    Editor.Document.Selection.CharacterFormat.Size = 20;
    Editor.Document.SetText(Windows.UI.Text.TextSetOptions.None, "");

}


来源:https://stackoverflow.com/questions/62128943/paragraph-mark-of-uwp-richeditbox-is-not-resized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!