WPF 生成文本块内联
我有一个 GridView,并且在其中一个 GridViewColumn 中想要生成如下文本:
textBlock.Text = string.Format("{0} is doing {1} .......", a, b);
但是 a
和 b
(视图中项目的属性)不应仅表示为纯文本,而应表示为超链接
。
(另外:格式文本应取决于项目的类型)
如何以这种方式生成 TextBlock
文本? (用于本地化)
问题更多:我应该自己写一些东西还是框架提供了一种简单的方法?
I have a GridView
and in one of the GridViewColumn
s i want to generate a text like this:
textBlock.Text = string.Format("{0} is doing {1} .......", a, b);
but a
and b
(Properties of an item in the View) should not just be represented as plain text, but as a Hyperlink
for example.
(Also: The format text should depend on the type of the item)
How can i generate the TextBlock
s text in that way? (for localization)
The Question is more: Should i write something on my own or is there an easy way provided by the framework?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个老问题,但我发现接受的答案绝对是矫枉过正。您根本不需要解析格式化文本!只需将其包裹在
Span
元素中即可。现在,您可以在代码中使用
FormattedText
附加属性:更重要的是,直接从 XAML 中使用:
其中
ns
是您定义Attached
的命名空间> 上课。An old question, but I find accepted answer an absolute overkill. You don't need to parse the formatted text at all! Just wrap it up in
Span
element and you are done.Now you can use the
FormattedText
attached property either in your code:More importantly, straight from the XAML:
Where
ns
is the namespace you defined theAttached
class in.最近我遇到了同样的问题。因此,我决定为
TextBlock
实现一个附加属性,它采用string
类型的值,然后动态填充Inlines
集合。您可以简单地将属性值设置为如下所示:还支持样式:
您还可以以标准方式在 XAML 中使用此附加属性。
以下是公开此属性的类的代码:
Recently i came across the same problem. So i decided to implement an attached property for
TextBlock
which takes a value ofstring
type and then populates theInlines
collection on the fly. You can simply set the property value to something like this:The styles are also supported:
You can also use this attached property in XAML in a standard way.
Here is the code of the class which exposes this property:
在 XAML 中你可以做这样的事情:
在代码后面可以做同样的事情,但我不推荐它,因为它涉及使用 FrameworkElementFactories。
In XAML you could do something like this:
In code behind the same thing can be done but i would not recommend it since it involves using FrameworkElementFactories.