sgonchar
Topic Author
Posts: 33
Joined: 15 Mar 2021, 22:11

html formatting in dynamic text

08 Apr 2021, 23:57

Hello,

There some cool formatting options here: https://www.noesisengine.com/docs/Gui.C ... orial.html

However, these don't seem to work via bindings defined as so:
        <TextBlock TextWrapping="Wrap"
            Style="{StaticResource BodyTextBlock}"
            Text="{Binding thatReallyLongText}"
          />
When "thatReallyLongText" is set to:
<Bold>Lorem ipsum dolor sit amet, </Bold> consectetur adipiscing elit. Donec scelerisque justo enim,
It just shows up with <Bold> right in the text.

Xaml gets very unhappy when I to use formatting tags directly in text ala:
        <TextBlock TextWrapping="Wrap"
            Style="{StaticResource BodyTextBlock}"
            Text="<Bold>Lorem ipsum dolor sit amet, </Bold> consectetur adipiscing elit. Donec scelerisque justo enim"
          />
I have to reformat it to:
        <TextBlock TextWrapping="Wrap"
            Style="{StaticResource BodyTextBlock}">
                <Bold>Lorem ipsum dolor sit amet, </Bold> consectetur adipiscing elit. Donec scelerisque justo enim"
          <TextBlock/>
However, I'm not terribly sure how to bind it to thatReallyLongText then .. Edit figured it out, but still no html parsing.


Question: I'm wondering what are our options for formatting a text that gets set via a binding?


Edit: Reformating via <textblock> binding didn't work ether, <bold> still just shows up as plain text.
          <TextBlock TextWrapping="Wrap"
            Style="{StaticResource BodyTextBlock}">
            <TextBlock.Text>
              {Binding thatReallyLongText}
            </TextBlock.Text>
          </TextBlock>
 
User avatar
sfernandez
Site Admin
Posts: 2255
Joined: 22 Dec 2011, 19:20

Re: html formatting in dynamic text

09 Apr 2021, 18:32

Hi,

A TextBlock doesn't directly understand that formatted text. What other clients are doing is creating an attached property or behavior that generates the inlines for the TextBlock. Something like this:
<TextBlock local:FormattedTextParser.Text="{Binding SomeFormattedText}"/>
The parsing process and inline generation will be done in the changed callback of that property:
void OnTextChanged(DependencyObject* d, const DependencyPropertyChangedEventArgs& e)
{
  TextBlock* textBlock = DynamicCast<TextBlock*>(d);
  if (textBlock != nullptr)
  {
    InlineCollection* inlines = textBlock->GetInlines();
    inlines->Clear();
    
    // Parse text...
    const String& text = *(const String*)e.newValue;
    
    // Add inlines...
  }
}
As it is something that has been asked many times we created a ticket in our bugtracker to define a sample behavior doing this: #1963
 
sgonchar
Topic Author
Posts: 33
Joined: 15 Mar 2021, 22:11

Re: html formatting in dynamic text

09 Apr 2021, 18:51

Thank you! Eagerly waiting for #1963

Who is online

Users browsing this forum: Google [Bot] and 3 guests