roncli.com blog
The blog of roncli
roncli.com blog
roncli.com
blog
Profile
roncli
Houston, Texas, United States
Labels
Coding
CTG Music
Editorials
Games
Miscellaneous
Music
Servers
Silliness
Software
Sports
Trax in Space Beta
Weather
Recent Posts
Sigh of Excitement
Programming is fun again
More XAML fun
The Nightstalker on Inner Space/KFOS
WCF and XBAP
Backup and Vista
Projects, Take 2
Time
Revisiting Numbers
Niagara Falls Roundup, eh?
Monday, December 10, 2007
Databinding to a XAML FlowDocument
Posted: 11:25:00 PM 0 comments
I couldn't find a full example of this, I had to put bits and pieces together before I got this working.

Let's say you have a datasource, and in that datasource, one of the datamembers is a string that contains some FlowDocument XAML. For example:

<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Paragraph>This is a test.</Paragraph>
</Section>

Unfortunately, there's no way to directly bind a string to a FlowDocument object. However, XAML databinding allows the use of custom converters that allows you to take that string and convert it to whatever you like. To do this, create a class:

Namespace Converters
 
    <ValueConversion(GetType(Object), GetType(FlowDocument))> Public Class FlowDocumentConverter
        Implements IValueConverter
 
        Public Function Convert(ByVal objValue As Object, ByVal tTarget As Type, ByVal objParam As Object, _
                    ByVal ciCulture As CultureInfo) As Object Implements IValueConverter.Convert
            Using msDocument As New MemoryStream((New ASCIIEncoding).GetBytes(CStr(objValue)))
                Dim fdDocument As New FlowDocument()
                Dim trDocument As New TextRange(fdDocument.ContentStart, fdDocument.ContentEnd)
                trDocument.Load(msDocument, DataFormats.Xaml)
                trDocument = Nothing
                Return fdDocument
            End Using
        End Function
 
        Public Function ConvertBack(ByVal objValue As Object, ByVal tTarget As Type, ByVal objParam As Object, _
                    ByVal ciCulture As CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return Nothing 'Not interested in converting back from a FlowDocument to a String
        End Function
 
    End Class
 
End Namespace

Now add your xmlns to the page/usercontrol/window/whatever...

<Page ... xmlns:converters="clr-namespace:Converters" ... >

Create yourself some resources...

<Page.Resources>
    <converter:FlowDocumentConverter x:Key="FlowDocumentConverter" />
</Page.Resources>

And bind like so...

<FlowDocumentScrollViewer Document="{Binding PropertyName, Converter={StaticResource FlowDocumentConverter}}" />

Just replace "PropertyName" with the name of the property or column that has the XAML FlowDocument string you wish to use.

It's a little counter-intuitive, it would be nice if you could just Document="{Binding PropertyName}" and be done with it, converting it to a FlowDocument on the fly... but the solution here is easy enough.

Labels: , , , ,




0 Comments

Post a Comment