6

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.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
Natalie Tay
  • 133
  • 12

1 Answers1

-1

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;
        }
    }
}
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • um, not exactly getting what you're doing there. i don't want the button to be removed, that's why i am checking at the PreviewKeyDown. from what i can see, you're adding a button when you're pressing 'B'? – Natalie Tay Apr 15 '12 at 19:53
  • Sorry I don't even remember anymore... my conclusion was that this control is just lame and shouldn't be used for anything more than what it gives out of the box. – justin.m.chase Apr 16 '12 at 20:33