Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Generate TextBlock Inlines

I have a GridView and in one of the GridViewColumns i want to generate a text like this:

textBlock.Text = string.Format("{0} is doing {1} .......", a, b);

but a and b (Properties of an item in the View) should not just be represented as plain text, but as a Hyperlink for example.
(Also: The format text should depend on the type of the item)

How can i generate the TextBlocks text in that way? (for localization)

The Question is more: Should i write something on my own or is there an easy way provided by the framework?

like image 612
ordag Avatar asked Apr 07 '11 14:04

ordag


People also ask

How do I create a line break in TextBlock WPF?

Adding Line Breaks Sometimes you will want to insert a line break within a TextBlock. You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element.

How do I add a paragraph in TextBlock WPF?

Add(new Run("Text of paragraph.")); // Create a span with the content of the paragraph (FontSize 25 and FontWeight Bold stay alive) Span span = new Span(para. ContentStart, para. ContentEnd); // Create a TextBlock with the span (FontSize 25 and FontWeight Bold get lost) TextBlock textBlock = new TextBlock(); textBlock.

How do I make text bold in WPF?

You can refer the following sample. string showmessage = "My name is John"; string Boldsplist = "is"; //Defines the bold field textBlock. Text = System. String.

What is the difference between TextBlock and label in WPF?

Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.


1 Answers

An old question, but I find accepted answer an absolute overkill. You don't need to parse the formatted text at all! Just wrap it up in Span element and you are done.

public class Attached
{
    public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
        "FormattedText", 
        typeof(string), 
        typeof(Attached), 
        new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure, FormattedTextPropertyChanged));

    public static void SetFormattedText(DependencyObject textBlock, string value)
    {
        textBlock.SetValue(FormattedTextProperty, value);
    }

    public static string GetFormattedText(DependencyObject textBlock)
    {
        return (string)textBlock.GetValue(FormattedTextProperty);
    }

    private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as TextBlock;
        if (textBlock == null)
        {
            return;
        }

        var formattedText = (string)e.NewValue ?? string.Empty;
        formattedText = string.Format("<Span xml:space=\"preserve\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">{0}</Span>", formattedText);

        textBlock.Inlines.Clear();
        using (var xmlReader = XmlReader.Create(new StringReader(formattedText)))
        {
            var result = (Span)XamlReader.Load(xmlReader);
            textBlock.Inlines.Add(result);
        }
    }
}

Now you can use the FormattedText attached property either in your code:

string inlineExpression = "<Run Style=\"Theme.GrayText\">Once I saw a little bird, go hop, hop, hop.</Run>";
Attached.SetFormattedText(myTextBlock1, inlineExpression);

More importantly, straight from the XAML:

<TextBlock ns:Attached.FormattedText="{Binding Content}" />

Where ns is the namespace you defined the Attached class in.

like image 105
gwiazdorrr Avatar answered Oct 25 '22 14:10

gwiazdorrr