-1

I have a RichEditBox and a TextBox. What I want to achieve is to "combine" the undo history of both controls, as if they were one text box. When the user performs an undo in one of both elements, it should actually occur in the control with the most recent event to undo.

For example, when I type some text in text box A and then some text in text box B before executing an undo in one of both elements, I want the input in B to be removed.

I have thought about the TextChanged event to build my own undo queue, but I don't see a possibility to group the entered characters like in RichEditBox. Undoing character by character is not a satisfactory user experience in my eyes.

Does anyone know a simple solution or a workaround for this problem?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
DasElias
  • 569
  • 8
  • 19

1 Answers1

0

Based on the document of RichEditBox, it mentions the content of a RichEditBox is a Windows.UI.Text.ITextDocument object and this ITextDocument interface provides a way to undo and redo changes and so on. So if you want to remove the text in RichEditBox when perform an undo in the TextBox, you can use ITextDocument interface to call the Undo method. For example, I subscribed the PreviewKeyDown event of TextBox, when press the Back key to perform an undo in the TextBox, remove the text in RichEditBox or remove the text in TextBox first.

.xaml:

<StackPanel>
    <RichEditBox x:Name="MyRichBox"></RichEditBox>
    <TextBox x:Name="MyTextBox" PreviewKeyDown="MyTextBox_PreviewKeyDown"></TextBox>
</StackPanel>

.cs:

private void MyTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Back) 
    {
        //delete
        if (MyTextBox.Text == "") 
        {
            MyRichBox.Document.Undo();
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • Hey, thanks for your answer, but unfortunately, your solution is not what I want to achieve. I have edited my question in order to clarify my issue. – DasElias May 22 '20 at 13:34
  • Maybe you can subscribe the TextChanged event for RichEditBox and TextBox, and define a list. When the TextChanged event triggers, add a string value in the list to identify the order of the characters you enter(e.g. "RichBox" and "TextBox" to indicate which box enters the character). In this case, when you press the 'Back' key, in the PreviewKeyDown event, you can judge which box you should undo based on the string from list. Then remove the text from the specific box. – Faywang - MSFT May 26 '20 at 07:26