简体   繁体   English

如何检查我是否正在删除(退格键、删除或“剪切”)RichTextBox 中的 UIElement?

[英]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.我有一个RichTextBox必须包含一些按钮,当用户在其中编辑内容时应该正确删除(处理)这些按钮。 I am able to check if I'm deleting (Backspace, Delete or Cut) text (characters) but not the <Button> control element.我能够检查我是否正在删除(Backspace、Delete 或 Cut)文本(字符),但不是<Button>控制元素。

Attached is the code I've used:附上我用过的代码:

XAML: 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#: 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.我知道start.GetTextInRun仅获取文本,并且在退格按钮时得到值“”( null )。 But I've tried start.GetAdjacentElement as well but I'm unable to succeed to retrieve a <Button> in the same condition.但我也尝试过start.GetAdjacentElement ,但我无法在相同条件下成功检索<Button>

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.我刚刚(大致)复制了上面的代码,当我将 cursor 放在添加的按钮之后并按 Backspace 时,即使没有 PreviewKeyDown 代码,按钮也会从 RichTextBox 中删除。 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM