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
WTF did Revival Productions just do?
A Tale of Two Communities
The Final Stretch
A Two Tiered, Untiered OTL
Secretly, you wish you could've done what I did
What have I done since roncli.com v2?
It's Done. It's Finally Done.
The Big Picture is Starting to Wear on Me
A Low Bang to Buck Ratio
win-acme
Archives
February 2005
March 2005
April 2005
May 2005
June 2005
July 2005
August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
February 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
November 2006
December 2006
February 2007
March 2007
April 2007
May 2007
June 2007
July 2007
August 2007
September 2007
October 2007
November 2007
December 2007
January 2008
February 2008
March 2008
April 2008
June 2008
July 2008
September 2008
December 2008
February 2009
July 2009
August 2009
September 2009
October 2009
November 2009
February 2010
March 2010
April 2010
June 2010
July 2010
August 2010
September 2010
October 2010
November 2010
December 2010
March 2011
June 2011
July 2011
August 2011
September 2011
October 2011
December 2011
January 2012
February 2012
April 2012
July 2012
November 2012
July 2013
April 2014
July 2014
August 2014
November 2014
December 2014
March 2015
April 2015
May 2015
June 2015
July 2015
September 2015
January 2016
February 2016
May 2016
July 2016
November 2016
March 2017
January 2018
May 2018
June 2018
January 2019
January 2021
February 2021
March 2021
August 2021
October 2021
December 2021
August 2022
November 2022
October 2023
February 2024
April 2024
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: , , , ,