Multiple text formatting in one TextBlock

kenosando#AC

New member
Jan 10, 2012
155
1
0
Visit site
My app parses XML data and displays it nicely with DataTemplate and embedded TextBlock. I have found ways to format the TextBlock with <Run> tags, but it doesn't appear to possible to add those at run-time. Here is my code:
Code:
<ListBox HorizontalAlignment="Left" Margin="-12,-40,0,0" Name="verses" Width="480" Height="620" SelectionChanged="enableSharing">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Name="verseBlock" Text="{Binding}" TextWrapping="Wrap" Width="15" HorizontalAlignment="Stretch" 
                                           DataContext="{Binding}" Tag="{Binding}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ListBox>

The text for the verses are displayed like this:
1 In the beginning ...


I create a List<string> in my code that builds each chapter into this format and displays it.
Essentially, I want just the verse number in bold, and the rest of the text in normal weight. I have seen examples that were non-specific to WP7, using WPF, but I am not sure if importing those libraries will work correctly on a WP device.

Any suggestions?
 

dotnetnelson

New member
Jun 12, 2012
13
0
0
Visit site
Let me save you some trouble: There's no easy way to do this. In WPF it was possible because there was such a thing as a FrameworkContentElement which you could do some black magic on, but its not the case here.

I solved this a while ago, but never really liked the solution. Basically (and I'm going from memory here) I created an attached property on a "Span" element of type INotifyCollectionChanged. I used the property metadata of the Attached Prop to be notified when the source changed (Think ItemsSource).

From there, I wired up the CollectionChanged event on the new value and manually added Runs to the Span when elements were added to the collection.

Like I said, very hideously ugly but it works. The implementation details are hidden from the UI through an attached property, so at least you don't pollute your View code behind.

I'll see if I can find some old code to provide.
 

Members online

Forum statistics

Threads
323,281
Messages
2,243,566
Members
428,056
Latest member
bevitalglucopre