WPF 中 DataBound 时将 TextBlock 设置为完全粗体
我有一个数据绑定 TextBlock 控件(在 DataTemplate 内部使用它来显示列表框中的项目),并且我想将控件中的所有文本设为粗体。 我似乎无法在属性资源管理器中找到将整个文本设置为粗体的属性,我在网上所能找到的只是在 TextBlock 内使用
标记,但我无法将其放入,因为数据直接来自数据源。
一定有办法做到这一点——但是怎么做呢? 我对 WPF 非常缺乏经验,所以我真的不知道该去哪里寻找。
I have a databound TextBlock control (which is being used inside a DataTemplate to display items in a ListBox) and I want to make all the text in the control bold. I can't seem to find a property in the properties explorer to set the whole text to bold, and all I can find online is the use of the <Bold>
tag inside the TextBlock, but I can't put that in as the data is coming directly from the data source.
There must be a way to do this - but how? I'm very inexperienced in WPF so I don't really know where to look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我是否遗漏了某些内容,或者您只需将 FontWeight 属性设置为“粗体”?
Am I missing something, or do you just need to set the FontWeight property to "Bold"?
不只是拥有一个 TextBlock,请尝试以下操作:
然后将数据绑定到 Run.TextProperty。
Rather than just having a TextBlock, try this:
Then databind to the Run.TextProperty instead.
你说数据直接来自数据源; 是否可以在它前面放置一个抽象层? 为您所显示的内容创建一个视图并让该视图与数据进行通信是很常见的。 这个想法最常见的实现是模型视图视图模型(MVVM)。 在线阅读相关内容。
您可能有一个绑定到文本框的“DisplayText”属性,它只是一个包装底层文本的“getter”。 它可以检测文本是否已经换行,如果没有,则换行。
例如。
然后,绑定到视图而不是直接绑定到对象。
You say that the data is coming directly from the datasource; is it possible to place a layer of abstraction in front of it? Its quite common to create a View for what you are displaying, and have the View communicate with the data. The most common implementation of this idea is Model View View-Model (MVVM). Have a read about it online.
You might have a 'DisplayText' property that is bound to the textbox, and it is simply a 'getter' that wraps the underlying text. It can detect if the text is already wrapped in and if not, wrap it.
Eg.
Then, bind to the View instead of directly to the object.