Silverlight TextBlock:将文本转换为 InlineCollection
考虑以下标记声明:
<TextBlock>
<Run>abcdefghijklmnopqrstuvwxyz</Run>
<LineBreak/>
<Run>0123456789</Run>
</TextBlock>
我想将任何数据绑定到 TextBlock 并将该数据转换为 InlineCollection。如果使用数据绑定来做到这一点会非常优雅。另一种方法是观察我的数据源并使用代码隐藏中 TextBlock 类的 Inlines-Property。
我尝试了以下方法,但没有成功:
<TextBlock>
<Binding Path="MyDataSource" Converter="{StaticResource MyTextConverter}"/>
</TextBlock>
我想要做的是通过将 TextBlock 封装在 ViewBox 中来实现自动字体缩放,而且在任意字母计数后生成 LineBreaks。
预先感谢您的任何帮助。 此致。
Consider the following markup declaration:
<TextBlock>
<Run>abcdefghijklmnopqrstuvwxyz</Run>
<LineBreak/>
<Run>0123456789</Run>
</TextBlock>
I want to bind any data to a TextBlock and convert this data to an InlineCollection. It would be very elegant to do this with Data Binding. The other way is to observe my Data Source and use the Inlines-Property from the TextBlock class in Code Behind.
I tried the following but it didn't work:
<TextBlock>
<Binding Path="MyDataSource" Converter="{StaticResource MyTextConverter}"/>
</TextBlock>
What I want to do is achieve auto font-scaling by encapsulating my TextBlock in a ViewBox, but also generating LineBreaks after an arbitrary letter count.
Thanks in advance for any help.
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我用了这个 回答 WPF 相关问题,提出一个 Silverlight 行为,该行为可以向 Silverlight 中的 TextBlock。格式化是通过 Xml 元素完成的,其工作方式类似于在元素本身内格式化 TextBlock。可以使用粗体、斜体、下划线和换行元素。
首先,行为定义如下:
然后,从xaml本身,可以如下引用行为:
I used this answer for a WPF related question to come up with a Silverlight Behavior that can add simple formatting to a TextBlock in Silverlight. Formatting is done through Xml elements that work similar to how a TextBlock can be formatted within the element itself. Bold, Italic, Underline, and LineBreak elements can be used.
First, the behavior is defined as follows:
Then, from the xaml itself, the behavior can be referenced as follows:
我想您只需将转换器分配给 TextBlock 的 TextProperty 即可。
然后,您的转换器添加
\r\n
以在需要时获得换行符。I guess you just have to assign yout converter to the TextProperty of your TextBlock.
Then your converter add
\r\n
to get a linebreak when you need it.我想我通过对 TextBlock 进行子类化以使 InlineCollection 可绑定并在 xaml 标记字符串和 InlineCollection(或内联的通用列表)之间编写转换器来回答这个问题]堆栈详细信息正如 infografnet 所指出的, Silverlight 版本的
TextBlock
类已被密封
,这使得我的WPF 子类建议无效。I think I answered this by sub-classing the TextBlock to make the InlineCollection bindable and writing a Convertor between a String of xaml markup and an InlineCollection(or generic list of Inlines)]Details here on the StackAs pointed out by infografnet, the Silverlight version of the
TextBlock
class issealed
which makes my WPF sub-classing suggestion invalid.这太容易让人难以置信了...
"\r\n"
在正确的位置就可以完成这项工作。It's too easy to be true ...
"\r\n"
at the right position does the job.