I am trying to get and set rich text in a ;RichEditBox, but every time I do perform a GetText then a SetText, one extra carriage return is added. Here the super simple example with a button that does get then set. Try it out to see one extra carriage return being added every time you perform the Get-Set.
XAML
<StackPanel>
<Button Content="Get-Set" Click="OnGetSet"/>
<RichEditBox x:Name="RichEditor" Width="300" Height="200"/>
</StackPanel>
C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
RichEditor.Document.GetText(TextGetOptions.FormatRtf, out value);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
I tried all sort of options both in the SetText and in the GetText, but I am able to prevent additional carriage return to be inserted. Does anyone have a suggestion?
I ended up finding a reasonable workaround. I am getting the full range of text, then invoke the GetText on the range instead of the document.
I am not sure if this is the best solution, but it works fine.
Updated C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
var value = GetText(RichEditor);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
public string GetText(RichEditBox editor)
{
// get the actual size of the text
editor.Document.GetText(TextGetOptions.UseLf, out string text);
// get the text in the total range - to avoid getting extra lines
var range = editor.Document.GetRange(0, text.Length);
range.GetText(TextGetOptions.FormatRtf, out string value);
// return the value
return value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With