Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a RichTextBox is empty

What is the best way to detect if a WPF RichTextBox/FlowDocument is empty?

The following works if only text is present in the document. Not if it contains UIElement's

new TextRange(Document.ContentStart, Document.ContentEnd).IsEmpty
like image 961
Benoit Dion Avatar asked Apr 28 '11 22:04

Benoit Dion


1 Answers

The answer above works if you don't put anything into the RTB. However, if you simply delete the contents, the RTB tends to return a single, empty paragraph, not a completely empty string. So, this is more reliable in such cases:

string text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
return !String.IsNullOrWhiteSpace(text);

This only applies to textual contents, of course.

like image 200
Gábor Avatar answered Sep 28 '22 07:09

Gábor