© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago
!🌸! Rushaan

Preventing TextBlock from getting deleted

I'm using a RichTextBox and setting some inline as TextBlock but the user is able to delete that textblock and i dont want that to happen, for some reason the inline returns Run type even if its a textblock, how do i see if its a textblock and prevent deletion

private void richTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back || e.Key == Key.Delete)
            {
                TextPointer caretPosition = rich.CaretPosition;
                if (caretPosition != null)
                {
                    TextPointer prevCaretPosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward);
                    if (prevCaretPosition != null)
                    {
                        Paragraph paragraph = prevCaretPosition.Paragraph;
                        if (paragraph != null)
                        {
                            Inline tb = paragraph.Inlines.LastInline;
                            if (tb != null) 
                            {
                                Trace.WriteLine(tb.GetType().ToString());
                            }
                        }
                    }
                }
            }
        }}
private void richTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Back || e.Key == Key.Delete)
            {
                TextPointer caretPosition = rich.CaretPosition;
                if (caretPosition != null)
                {
                    TextPointer prevCaretPosition = caretPosition.GetNextInsertionPosition(LogicalDirection.Backward);
                    if (prevCaretPosition != null)
                    {
                        Paragraph paragraph = prevCaretPosition.Paragraph;
                        if (paragraph != null)
                        {
                            Inline tb = paragraph.Inlines.LastInline;
                            if (tb != null) 
                            {
                                Trace.WriteLine(tb.GetType().ToString());
                            }
                        }
                    }
                }
            }
        }}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

WPF TextBlock Text binding
C#CC# / help
2y ago
Unable to Populate XAML TextBlock with Data from JSON File
C#CC# / help
3y ago
❔ Preventing a panel from overflowing + show scrollbar WPF
C#CC# / help
3y ago