I have RichTextBoxone that should contain some buttons that should be removed properly (processed) when the user edits the content in it. I can check if I am deleting (Backspace, Delete or Cut) text (characters) but not the control <Button>.
The attached code that I 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>
WITH#:
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());
}
and 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);
}
}
I understand that it only start.GetTextInRungets the text, and I get the value "" ( null) when the button is clicked back. But I also tried start.GetAdjacentElement, but I was not able to get <Button>in the same condition.