如何检查我是否正在删除(退格键、删除或“剪切”)RichTextBox 中的 UIElement?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是(粗略地)复制了上面的代码,当我将光标放在添加的按钮后面并按 Backspace 时,即使没有 PreviewKeyDown 代码,该按钮也会从 RichTextBox 中删除。所以我很困惑,因为答案似乎就是你根本不需要做任何事情。这是我的代码:
以及背后的代码......
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:
and the code behind...