简体   繁体   中英

How to check if I'm deleting (Backspace, Delete or 'Cut') a UIElement in a RichTextBox?

I have a RichTextBox that has to contain some buttons which should be deleted properly (handled) when the user is editing content in it. I am able to check if I'm deleting (Backspace, Delete or Cut) text (characters) but not the <Button> control element.

Attached is the code I've used:

XAML:

<RichTextBox x:Name="tRTB"
             HorizontalAlignment="Left"
             Keyboard.PreviewKeyDown="tRTB_PreviewKeyDown"
             PreviewTextInput="tRTB_PreviewTextInput">
   <local:EnabledFlowDocument x:Name="tFD">
      <Paragraph x:Name="tP"/>                 
   </local:EnabledFlowDocument>
</RichTextBox>

C#:

public void AppendNewButton(int i)
{
    Button addB = new Button();
    addB.Content = i;
    addB.HorizontalAlignment = HorizontalAlignment.Left;

    tP.Inlines.Add(addB);
    tP.Inlines.Add("Bk" + i.ToString()); //appends a button and text in RTB
}

and the event:

private void tRTB_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Back)
    {
        var start = tRTB.CaretPosition;
        var t = start.GetTextInRun(LogicalDirection.Backward);
        var end = start.GetNextContextPosition(LogicalDirection.Backward);
        var t1 = end.GetTextInRun(LogicalDirection.Backward);

        tRTB.Selection.Select(start, end);
        tRTB.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);
        tRTB.Selection.Select(start, start);

        //should handle deletion of button here
        /* if (button is before cursor) */
        /* e.Handled=true; */
    }
}

I understand that the start.GetTextInRun only gets the text, and I get the value "" ( null ) when backspacing a button. But I've tried start.GetAdjacentElement as well but I'm unable to succeed to retrieve a <Button> in the same condition.

I just replicated your code above (roughly) and when I put the cursor after the added button and hit Backspace the button is removed from the RichTextBox even without your PreviewKeyDown code. So I'm very confused because it seems like the answer is just that you don't need to do anything at all. Here's my code:

<Window x:Class="WpfApplication4.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525">
  <Grid>
    <RichTextBox 
      x:Name="tRTB"
      HorizontalAlignment="Left"
      Keyboard.PreviewKeyDown="tRTB_PreviewKeyDown">
    </RichTextBox>
  </Grid>
</Window>

and the code behind...

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void AppendNewButton(int i)
    {
        Button addB = new Button();
        addB.Content = i;
        addB.HorizontalAlignment = HorizontalAlignment.Left;

        var insertionPosition = tRTB.CaretPosition.GetInsertionPosition(LogicalDirection.Forward);

        var inline = new InlineUIContainer(addB);
        insertionPosition.Paragraph.Inlines.InsertAfter(
            (Inline)insertionPosition.Parent,
            inline);

        tRTB.CaretPosition = tRTB.CaretPosition.GetNextInsertionPosition(LogicalDirection.Forward);
    }

    public void tRTB_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.B)
        {
            AppendNewButton(7);
            e.Handled = true;
        }
    }
}

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