Detect if a RichTextBox is empty

后端 未结 5 2729
时光取名叫无心
时光取名叫无心 2021-02-20 01:15

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

5条回答
  •  故里飘歌
    2021-02-20 02:14

    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.

提交回复
热议问题