[英]Formatting text in a TextBlock
How do I achieve formatting of a text inside a TextBlock
control in my WPF application?如何在 WPF 应用程序的
TextBlock
控件中实现文本格式?
eg: I would like to have certain words in bold, others in italic, and some in different colors, like this example:例如:我希望某些单词为粗体,其他为斜体,有些为不同的颜色,例如以下示例:
The reason behind my question is this actual problem:我的问题背后的原因是这个实际问题:
lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
I would like the second part of the string to be bold, and I know that I could use two controls (Labels, TextBlocks, etc.) but I'd rather not, due the vast amount of controls already in use.我希望字符串的第二部分为粗体,并且我知道我可以使用两个控件(标签、文本块等),但我宁愿不使用,因为已经有大量控件在使用。
You need to use Inlines
:您需要使用
Inlines
:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="This is WPF TextBlock Example. " />
<Run FontStyle="Italic" Foreground="Red" Text="This is red text. " />
</TextBlock.Inlines>
With binding:带绑定:
<TextBlock.Inlines>
<Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
<Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
</TextBlock.Inlines>
You can also bind the other properties:您还可以绑定其他属性:
<TextBlock.Inlines>
<Run FontWeight="{Binding Weight}"
FontSize="{Binding Size}"
Text="{Binding LineOne}" />
<Run FontStyle="{Binding Style}"
Foreground="Binding Colour}"
Text="{Binding LineTwo}" />
</TextBlock.Inlines>
You can bind through converters if you have bold as a boolean (say).如果您将粗体作为布尔值(例如),则可以通过转换器进行绑定。
You can do this in XAML easily enough:你可以很容易地在 XAML 中做到这一点:
<TextBlock>
Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
</TextBlock>
There are various Inline
elements that can help you, for the simplest formatting options you can use Bold
, Italic
and Underline
:有各种
Inline
元素可以帮助您,对于最简单的格式选项,您可以使用Bold
、 Italic
和Underline
:
<TextBlock>
Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>
I think it is worth noting, that those elements are in fact just shorthands for Span
elements with various properties set (ie: for Bold
, the FontWeight
property is set to FontWeights.Bold
).我认为值得注意的是,这些元素实际上只是具有各种属性设置的
Span
元素的简写(即:对于Bold
, FontWeight
属性设置为FontWeights.Bold
)。
This brings us to our next option: the aforementioned Span
element.这将我们带到了下一个选项:前面提到的
Span
元素。
You can achieve the same effects with this element as above, but you are granted even more possibilities;您可以使用此元素实现与上述相同的效果,但您获得了更多的可能性; you can set (among others) the
Foreground
or the Background
properties:您可以设置(除其他外)
Foreground
或Background
属性:
<TextBlock>
Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>.
</TextBlock>
The Span
element may also contain other elements like this: Span
元素还可以包含其他元素,如下所示:
<TextBlock>
<Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>
There is another element, which is quite similar to Span
, it is called Run
.还有另一个元素,它与
Span
非常相似,称为Run
。 The Run
cannot contain other inline elements while the Span
can, but you can easily bind a variable to the Run
's Text
property: Run
不能包含其他内联元素,而Span
可以,但您可以轻松地将变量绑定到Run
的Text
属性:
<TextBlock>
Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>
Also, you can do the whole formatting from code-behind if you prefer:此外,如果您愿意,您可以从代码隐藏进行整个格式化:
TextBlock tb = new TextBlock();
tb.Inlines.Add("Sample text with ");
tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold });
tb.Inlines.Add(", ");
tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic });
tb.Inlines.Add("and ");
tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline });
tb.Inlines.Add("words.");
Check out this example from Charles Petzolds Bool Application = Code + markup从 Charles Petzolds 中查看此示例 Bool Application = Code + markup
//----------------------------------------------
// FormatTheText.cs (c) 2006 by Charles Petzold
//----------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Documents;
namespace Petzold.FormatTheText
{
class FormatTheText : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new FormatTheText());
}
public FormatTheText()
{
Title = "Format the Text";
TextBlock txt = new TextBlock();
txt.FontSize = 32; // 24 points
txt.Inlines.Add("This is some ");
txt.Inlines.Add(new Italic(new Run("italic")));
txt.Inlines.Add(" text, and this is some ");
txt.Inlines.Add(new Bold(new Run("bold")));
txt.Inlines.Add(" text, and let's cap it off with some ");
txt.Inlines.Add(new Bold(new Italic (new Run("bold italic"))));
txt.Inlines.Add(" text.");
txt.TextWrapping = TextWrapping.Wrap;
Content = txt;
}
}
}
a good site, with good explanations:一个很好的网站,有很好的解释:
http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/ http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/
here the author gives you good examples for what you are looking for!在这里,作者为您提供了您正在寻找的好例子! Overal the site is great for research material plus it covers a great deal of options you have in WPF
总的来说,该站点非常适合研究材料,而且它涵盖了您在 WPF 中拥有的大量选项
Edit编辑
There are different methods to format the text.有不同的方法来格式化文本。 for a basic formatting (the easiest in my opinion):
对于基本格式(我认为最简单):
<TextBlock Margin="10" TextWrapping="Wrap">
TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
</TextBlock>
Example 1 shows basic formatting with Bold Itallic and underscored text.示例 1 显示了带有粗斜体和下划线文本的基本格式。
Following includes the SPAN method, with this you van highlight text:以下包括 SPAN 方法,您可以使用此方法突出显示文本:
<TextBlock Margin="10" TextWrapping="Wrap">
This <Span FontWeight="Bold">is</Span> a
<Span Background="Silver" Foreground="Maroon">TextBlock</Span>
with <Span TextDecorations="Underline">several</Span>
<Span FontStyle="Italic">Span</Span> elements,
<Span Foreground="Blue">
using a <Bold>variety</Bold> of <Italic>styles</Italic>
</Span>.
</TextBlock>
Example 2 shows the span function and the different possibilities with it.示例 2 显示了跨度函数及其不同的可能性。
For a detailed explanation check the site!有关详细说明,请查看网站!
This is my solution....这是我的解决方案......
<TextBlock TextWrapping="Wrap" Style="{DynamicResource InstructionStyle}">
<Run Text="This wizard will take you through the purge process in the correct order." FontWeight="Bold"></Run>
<LineBreak></LineBreak>
<Run Text="To Begin, select" FontStyle="Italic"></Run>
<Run x:Name="InstructionSection" Text="'REPLACED AT RUNTIME'" FontWeight="Bold"></Run>
<Run Text="from the menu." FontStyle="Italic"></Run>
</TextBlock>
I am learning... so if anyone has thaughts on the above solution please share!我正在学习......所以如果有人对上述解决方案有想法,请分享! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.