wpf - Detect if a RichTextBox is empty -


what best way detect if wpf richtextbox/flowdocument empty?

the following works if text present in document. not if contains uielement's

new textrange(document.contentstart, document.contentend).isempty 

you compare pointers, not reliable:

var start = rtb.document.contentstart; var end = rtb.document.contentend; int difference = start.getoffsettoposition(end); 

this evaluates 2 if rtb loaded, , 4 if content has been entered , removed again.
if rtb cleared out e.g. via select -> delete value 0.


in silverlight reference on msdn method found can adapted , improved to:

public bool isrichtextboxempty(richtextbox rtb) {     if (rtb.document.blocks.count == 0) return true;     textpointer startpointer = rtb.document.contentstart.getnextinsertionposition(logicaldirection.forward);     textpointer endpointer = rtb.document.contentend.getnextinsertionposition(logicaldirection.backward);     return startpointer.compareto(endpointer) == 0; } 

Comments