Display curly braces in RichTextBox when manually setting Xaml property

廉价感情. 提交于 2019-12-11 11:26:11

问题


I'm currently working on a Twitter app for Windows Phone, in which – via a custom DependencyProperty – the Xaml property of each RichTextBox is bound to the text of a tweet (which includes programmatically generated Run elements with all the text). This works good enough until there are curly braces { and } in the text. I tried replacing them with { and escaping with {} but to no avail.

The weird thing is, if the curly braces show up in the middle of the text (more specifically, in the Text property of a Run element) everything's fine, it's only when an opening curly brace is at the beginning* or a closing curly brace is at the end* that the RichTextBox crashes. I was able to work around this by inserting zero-width spaces with ​ at the beginning and end of each Run.

However, this issue gets more complicated if there is both an opening curly brace at the beginning* and a closing curly brace at the end*. In that case, the zero-width spaces don't seem to work anymore, so the only workaround would be to insert additional "real" characters at either the beginning or end of the text, which I obviously can't do.

This does seem to be a bug with RichTextBox, as acknowledged in this answer over two years ago. Apparently, it hasn't been fixed yet. So, does anyone know of any further workarounds I could try?

* can also be preceeded only by whitespace or followed only by whitespace


回答1:


Okay after some more tinkering I found an easy fix. Just put a "zero-width non-joiner" character ‌ (‌ doesn't work in XAML) at the beginning and end of each Run.

Apparently this character is treated differently than the zero-width space I used previously and even works if your text has curly braces at both the beginning and the end. There are of course even more invisible characters that have the same effect (check this list on Wikipedia) but the zero-width non-joiner should have the least side-effects I think.



来源:https://stackoverflow.com/questions/12098250/display-curly-braces-in-richtextbox-when-manually-setting-xaml-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!