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

发布于 2024-11-23 21:12:19 字数 1745 浏览 2 评论 0原文

我有一个 RichTextBox ,它必须包含一些按钮,当用户编辑其中的内容时,应正确删除(处理)这些按钮。我可以检查是否删除(退格键、删除或剪切)文本(字符),但不能检查

附件是我使用过的代码:

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
}

和事件:

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; */
    }
}

我知道 start.GetTextInRun 仅获取文本,并且我得到值“”(null code>) 退格按钮时。但我也尝试过 start.GetAdjacentElement ,但无法在相同条件下成功检索

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-11-30 21:12:19

我只是(粗略地)复制了上面的代码,当我将光标放在添加的按钮后面并按 Backspace 时,即使没有 PreviewKeyDown 代码,该按钮也会从 RichTextBox 中删除。所以我很困惑,因为答案似乎就是你根本不需要做任何事情。这是我的代码:

<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>

以及背后的代码......

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;
        }
    }
}

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文