views:

42

answers:

1

I have a large chunk of customer relevant information that I am storing in an XML file.

Each XML element requires some sort of rich text formatting (bold, italics, new line, paragraphs, etc...). I have complete control over the XML file (i.e. I can wrap the text in other XML elements if required), and it is static (which makes life a bit easier).

What is a good way to store the information in the XML file so that I can load it into my Silverlight page with the correct formatting?

For Example:

I have a string like so:

var str = @"<Run Foreground=""Maroon"" FontFamily=""Courier New"" FontSize=""24"">Courier New 24</Run>";

I cannot just do this:

MyTextBlock.Text = str;

because it prints out literally as the string is defined (no formatting)

However, in XAML, I can define the TextBlock like so:

<TextBlock x:Name="PageDetailsTextBlock">
    <Run Foreground="Maroon" FontFamily="Courier New" FontSize="24">Courier New 24</Run>
</TextBlock>

And the XAML parser will convert that into the correctly formatted version.

How can do this in C#?

+1  A: 

The Xaml parser needed a parent object and a Xaml namespace, but I have created an extension method, for TextBlocks, to do the work for you:

using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;

namespace SilverlightApplication1
{
    static public class TextBlockExtensions
    {
        static public void SetInline(this TextBlock textBlock, string text)
        {
            var str = @"<TextBlock xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""&gt;" + text + "</TextBlock>";
            TextBlock txt = (TextBlock)XamlReader.Load(str);
            List<Inline> inlines = txt.Inlines.ToList();
            txt.Inlines.Clear();
            foreach (var inline in inlines)
            {
                textBlock.Inlines.Add(inline);
            }
        }
    }
}

You can now just say:

MyTextBlock.SetInline(str);

I tested it and it works fine for any normal Xaml text runs. Enjoy.

Enough already
Thanks very much for that... so just to go one step further, I have bound the `Text` property of my TextBlock to a property in my ViewModel, which is the raw XAML, how can I inject your extension method so that when the `Text` property gets populated, it actually changes the TextBlock to have inlines instead?
Mark
You could do binding like that using an attached property. The setter would call the extension method. I will post an example later. Glad it was helpful.
Enough already
Interesting, that could work, but for now I added a Loaded event to the textblock and did the changes in there, which seemed to work. But if you have time, i would still like to see you other example.
Mark
@Mark: Attached properties are great (we have used them for some very cool things not normally associated with APs). I will try get to it on the weekend, as I will be making good use of the idea you started :) Will post the attached property code sometime soon. Cheers
Enough already