realm_mike
Topic Author
Posts: 5
Joined: 05 May 2023, 06:03

Binding to Inlines of TextBlock

27 May 2023, 17:47

In the TextBlock documentation I see:
A TextBlock can contain a string in its Text property or Inline flow content elements, such as Bold, Hyperlink, and InlineUIContainer, in its Inlines property.
So is it possible to bind an inlines collection to the Inlines property?
                    <TabControl TabStripPlacement="Left" x:Name="TellsTabs">
                        <TabControl.ItemTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Sender}" />
                            </DataTemplate>
                        </TabControl.ItemTemplate>
                        <TabControl.ContentTemplate>
                            <DataTemplate>
                                <TextBlock Inlines="{Binding Text}" />
                            </DataTemplate>
                        </TabControl.ContentTemplate>
                    </TabControl>
struct RealmUI_Tell : Noesis::BaseComponent
{
	Noesis::String sender;
	Noesis::Ptr<Noesis::InlineCollection> text = Noesis::MakePtr<Noesis::InlineCollection>();

	const Noesis::String& GetSender() const	{ return sender; }
	Noesis::InlineCollection* GetText() const { return text; }

	NS_IMPLEMENT_INLINE_REFLECTION(RealmUI_Tell, Noesis::BaseComponent)
	{
		NsProp("Sender", &RealmUI_Tell::GetSender);
		NsProp("Text", &RealmUI_Tell::GetText);
	}
};
I don't see any errors being logged from Noesis, but the inline elements also don't show up in the textblock. Is this sort of thing possible to do?
 
User avatar
sfernandez
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: Binding to Inlines of TextBlock

29 May 2023, 13:54

The Inlines property is read-only. When assigning a binding it just tries to add it to the collection, and Noesis outputs an error saying "Cannot add a 'BindingExpression' to a 'InlineCollection'".

Maybe you are trying to achieve something similar to our RichText extension, which automatically generates the inlines from a formatted text. Is that what you are looking for?
 
realm_mike
Topic Author
Posts: 5
Joined: 05 May 2023, 06:03

Re: Binding to Inlines of TextBlock

29 May 2023, 17:23

Similar to RichText, but not quite the same I think. For one, the actual TextBlock is inside a DataTemplate so I was hoping to use bindings to get at the data rather than needing the C++ code to get at the actual UI element.

Secondly, this is a TextBlock that I want to append lines to over time, so I don't really want a single 'text' property that has to re-parse the entire text every time a new line is added. The Inlines collection seemed to handle this pretty well where I could create a Span representing the new line to add, and then just Add it to the inlines collection.

I suppose the other way I could do this would be to do something like what the RichText control does, but in the 'getter' for the text on the view model class, it could be a destructive Get, so that any time the Text property changes, the UI 'gets' the text but treats it as text to append. It's a bit of an awkward design but I suppose it could work.

Is there a reason the inline collection must be read-only and can't support coming from bindings?
 
User avatar
sfernandez
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: Binding to Inlines of TextBlock

31 May 2023, 17:58

You can have an attached property that can be bound to an observable collection of lines. And then the attached property will fill the InlineCollection as you said, adding a Span for each line. And even listen to changes in the observable collection to update the inlines without needing to reparse any text.
<TextBlock local:TextHelper.Lines="{Binding TextLines}" .../>
Is there a reason the inline collection must be read-only and can't support coming from bindings?
The Inlines collection is created by the TextBlock itself and interacts with the TextBlock, so it cannot be something external.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest