格式化TextBlock中的文本

Ash*_*Ash 94 c# wpf wpf-controls

如何TextBlock在WPF应用程序中实现控件内文本的格式化?

例如:我想要粗体显示某些单词,斜体显示某些单词,有些颜色不同,例如:

在此输入图像描述

我的问题背后的原因是这个实际问题:

lblcolorfrom.Content = "Colour From: " + colourChange.ElementAt(3).Value.ToUpper();
Run Code Online (Sandbox Code Playgroud)

我希望字符串的第二部分是粗体,我知道我可以使用两个控件(Labels,TextBlocks等),但我不愿意,因为已经使用了大量的控件.

Chr*_*isF 129

你需要使用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>
Run Code Online (Sandbox Code Playgroud)

有约束力:

<TextBlock.Inlines>
    <Run FontWeight="Bold" FontSize="14" Text="{Binding BoldText}" />
    <Run FontStyle="Italic" Foreground="Red" Text="{Binding ItalicText}" />
</TextBlock.Inlines>
Run Code Online (Sandbox Code Playgroud)

您还可以绑定其他属性:

<TextBlock.Inlines>
    <Run FontWeight="{Binding Weight}"
         FontSize="{Binding Size}"
         Text="{Binding LineOne}" />
    <Run FontStyle="{Binding Style}"
         Foreground="Binding Colour}"
         Text="{Binding LineTwo}" />
</TextBlock.Inlines>
Run Code Online (Sandbox Code Playgroud)

如果你将粗体作为布尔值(例如),你可以通过转换器绑定.


Ash*_*vis 95

您可以在XAML中轻松完成此操作:

<TextBlock>
  Hello <Bold>my</Bold> faithful <Underline>computer</Underline>.<Italic>You rock!</Italic>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • @ArsenMkrt怎么样:<TextBlock FontWeight ="Bold"Text ="{Binding Budget}"/> (10认同)
  • 这支持绑定吗? (6认同)
  • @Aetherix我无法正常工作。我从qqbenq使用它:&lt;TextBlock&gt; &lt;Bold&gt;£&lt;/ Bold&gt; &lt;Run FontWeight =“ Bold” Text =“ {Binding MonthlyPayment}” /&gt; &lt;/ TextBlock&gt; (2认同)

qqb*_*enq 45

有各种各样的Inline元素可以帮助您,您可以使用最简单的格式化选项Bold,Italic并且Underline:

<TextBlock>
    Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我认为值得注意的是,这些元素实际上只是Span具有各种属性集的元素的缩写(即:for Bold,FontWeight属性设置为FontWeights.Bold).

这将我们带到下一个选项:前面提到的Span元素.

您可以使用上面的元素实现相同的效果,但您可以获得更多的可能性; 你可以设置(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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Span元素还可能包含其他元素,如下所示:

<TextBlock>
    <Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

还有另一个元素Span,它被称为非常相似Run.将Run不能包含,而其他内联元素Span也可以,但你可以很容易的变量绑定到RunText属性:

<TextBlock>
    Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

此外,如果您愿意,可以从代码隐藏中执行整个格式化:

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.");
Run Code Online (Sandbox Code Playgroud)


Weg*_*ged 43

查看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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gie*_*lez 7

一个好的网站,有很好的解释:

http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/

在这里,作者为您提供了很好的例子来满足您的需求!整个网站非常适合研究材料,而且它涵盖了WPF中的大量选项

编辑

格式化文本有不同的方法.对于基本格式(在我看来最简单):

    <TextBlock Margin="10" TextWrapping="Wrap">
                    TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> text.
    </TextBlock>
Run Code Online (Sandbox Code Playgroud)

示例1显示了Bold Itallic和下划线文本的基本格式.

以下包括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>
Run Code Online (Sandbox Code Playgroud)

示例2显示了跨度函数及其不同的可能性.

有关详细说明,请查看网站!

例子