[英]Set TextBlock to be entirely bold when DataBound in 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. 我有一个数据绑定TextBlock控件(在DataTemplate中使用它来显示ListBox中的项目),我想让控件中的所有文本变为粗体。 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. 我似乎无法在属性资源管理器中找到一个属性来将整个文本设置为粗体,我在网上找到的只是在TextBlock中使用
<Bold>
标签,但我不能把它放在数据直接来自数据源。
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.
我对WPF很缺乏经验,所以我真的不知道在哪里看。
我错过了什么,或者你只需要将FontWeight属性设置为“Bold”?
<TextBlock FontWeight="Bold" Text="{Binding Foo}" />
Rather than just having a TextBlock, try this: 不要只是拥有一个TextBlock,试试这个:
<TextBlock>
<Bold>
<Run />
</Bold>
</TextBlock>
Then databind to the Run.TextProperty instead. 然后将数据绑定到Run.TextProperty。
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.
为您正在显示的内容创建视图并让View与数据通信非常常见。 The most common implementation of this idea is Model View View-Model (MVVM).
这种想法最常见的实现是模型视图视图模型(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. 您可能有一个绑定到文本框的“DisplayText”属性,它只是一个包含底层文本的“getter”。 It can detect if the text is already wrapped in and if not, wrap it.
它可以检测文本是否已经包装,如果没有,请将其包装。
Eg. 例如。
public class TestView {
private Test datasource;
public TestView(Test source)
{
this.datasource = source;
}
public string DisplayText {
get {
if (datasource.Text.Contains("<bold>")==false) {
return "<bold>" + datasource.Text + "</bold>";
}
return datasource.Text;
}
}
}
Then, bind to the View instead of directly to the object. 然后,绑定到View而不是直接绑定到对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.