如何在Silverlight中使TextBlock文本加粗?

9

我正在使用C#开发Windows Phone 7应用程序。我对Windows Phone 7应用程序和Silverlight都不熟悉。我想动态生成Texblock的粗体文本。我只想为部分文本生成粗体文本。我正在使用以下代码:

IncometextBlock.Text = "Income entries on " + selectedDate.ToShortDateString() + "        Page - "+SelectedButtonName+"";

我希望输出结果为:
"2011年1月21日收入记录 页码-A"
如何使文本加粗以满足上述要求?您能否为我提供任何代码或链接来解决上述问题?如果我做错了什么,请指导我。
2个回答

24

我会这样做。

IncometextBlock.Inlines.Clear();
IncometextBlock.Inlines.Add(new Run() {Text = "Income entries", FontWeight = FontWeights.Bold});
IncometextBlock.Inlines.Add(new Run() {Text = " on " }); 
IncometextBlock.Inlines.Add(new Run() {Text = selectedDate.ToShortDateString(), FontWeight = FontWeights.Bold});
IncometextBlock.Inlines.Add(new Run() {Text = "     Page - "}); 
IncometextBlock.Inlines.Add(new Run() {Text = SelectedButtonName, FontWeight = FontWeights.Bold});

如果您的数据是数据绑定的呢? - Code Maverick

7
如果你使用WrapPanel(来自工具包),你可以这样做:
<Grid>
    <toolkit:WrapPanel>
        <TextBlock Text="Income entries" FontWeight="Bold"/>
        <TextBlock Text=" on "/>
        <TextBlock Text="21/01/2011" FontWeight="Bold"/>
        <TextBlock Text=" Page - "/>
        <TextBlock Text="A" FontWeight="Bold"/>
    </toolkit:WrapPanel>
</Grid>

(上述仅为网格,以便在SO中启用代码高亮显示,不需要实现效果。)

3
WP7支持文本块中的“Run”文本元素,而文本块可以更有效地处理文本换行。在我看来,使用工具包来解决这个小问题有些过头了。 - AnthonyWJones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接