检测 RichTextBox 是否为空
检测 WPF RichTextBox/FlowDocument 是否为空的最佳方法是什么?
如果文档中仅存在文本,则以下内容有效。如果它包含 UIElement 则不是
new TextRange(Document.ContentStart, Document.ContentEnd).IsEmpty
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以比较指针,但这不太可靠:
如果加载了 RTB,则计算结果为
2
;如果内容已输入并再次删除,则计算结果为4
。如果 RTB 被完全清除,例如通过
select all ->删除
值将为0
。在 MSDN 上的 Silverlight 参考中,另一个发现可以调整和改进的方法:
You could compare the pointers, which is not all too reliable:
This evaluates to
2
if the RTB is loaded, and4
if content has been entered and removed again.If the RTB is completely cleared out e.g. via
select all -> delete
the value will be0
.In the Silverlight reference on MSDN another method is found which can be adapted and improved to:
如果您需要区分图像和空白,HB 的答案没有用。您可以使用此答案之类的内容来检查图像。
这看起来很费力,而且可能仍然不适用于所有情况。但我找不到更好的办法。
H.B.'s answer isn't useful if you need to distinguish between images and whitespace. You can use something like this answer to check for images.
This seems laborious, and still probably isn't correct for all scenarios. But I couldn't find any better way.
如果您不向 RTB 中添加任何内容,则上述答案有效。但是,如果您只是删除内容,RTB 往往会返回单个空段落,而不是完全空的字符串。因此,在这种情况下,这更可靠:
当然,这仅适用于文本内容。
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:
This only applies to textual contents, of course.
首先 - 感谢 McGarnagle - 他们的回答让我朝着正确的方向前进。然而,无论出于何种原因,他们的图像检查对我不起作用。这就是我最终要做的:
可能还有其他检查要做,但这至少涵盖了文本、图像和表格。
First - thank you to McGarnagle - their answer got me going in the right direction. However for whatever reason their image check didn't work for me. This is what I ended up doing:
there may be other checks to do, but this at least covers text, images and tables.
这是 HB 想法的延伸,适用于文本和图像。
我发现只要 RTB 有文本,差异总是大于 4。但是,如果您只粘贴图像,则为 3。为了解决这个问题,我查看了原始 rtf 字符串的字符串长度。
通过我的测试,我发现一个没有字符的空框的长度为 270。如果我粘贴一张只有 1 像素大小的图像,它的长度就会膨胀到 406。
我在不输入任何字母的情况下切换了各种格式选项,并避风港还没有接近 300,所以我选择了 350 作为基线。
如果没有文本字符,但它们粘贴在巨大的图像中,长度检查可能会很昂贵。
Here's an extension of H.B.'s idea that works with both text and images.
I found that difference is always >4 whenever the RTB has text. However, if you only paste an image it is 3. To combat this i look at the string length of the raw rtf string.
Through my testing i found that an empty box with no chars has a length of 270. If i even paste in an image that's only 1 pixel in size it balloons to 406.
I played with toggling on various formatting options without typing any letters and haven't gotten close to 300, so I went with 350 for the baseline.
The length check could be expensive if there are no textual characters, but they pasted in a massive image.