Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / WPF

WPF Flow Document For Beginners

Rate me:
Please Sign up or sign in to vote.
4.70/5 (26 votes)
17 Jun 2009CPOL2 min read 164.8K   49   8
WPF Flow document for beginners

As part of an article that I am creating for www.codeproject.com, I decided to look into using the System.Windows.Documents namespace and have a look at seeing if I could make a semi-cool’ish looking document.

Now when you use FlowDocuments, there are several container WPF container controls which you may host a FlowDocument in. These WPF container controls vary in what they provide. Let's see the difference, shall we?

  • FlowDocumentScrollViewer: Simply displays the entire document and provides a scroll bar -  like a web page
  • FlowDocumentPageViewer: Shows document as individual pages, and allows user to adjust zoom level
  • FlowDocumentReader: Combines FlowDocumentScrollViewer and FlowDocumentPageViewer into a single control, and exposes text search facilities

For example, a FlowDocumentPageViewer is as shown below:

37368/flowdocumentpageviewer.png

For those of who have not come across the FlowDocument, here is a list of some of the things that can be done with it:

  • Allow paragraphing
  • Allow anchoring of images
  • Allow hyperlinks
  • Allow text blocks
  • Allow tables
  • Allow subscript/superscript text
  • Allow UIElements (such as Button, etc.)
  • Allow text effects

Think of FlowDocument(s) as a mini desktop publishing type interface. Though I’m sure things like Quark are going to yield more flexibility. Nevertheless, the results of FlowDocument(s) could be thought as being able to create that sort of page publishing type layout.

What I’m going to do now is show you how to create a few of the various FlowDocument elements both in XAML and in code as they are little different actually.

Paragraph

In XAML

XML
<!– FLOW DOCUMENT VIEWER START >    
<FlowDocumentPageViewer x:Name="flowDocViewer"  Margin="0,0,0,0″ 
    Background="#FF414141″ Zoom="80″ >         
<!– FLOW DOCUMENT START –>
<FlowDocument x:Name="flowDoc" Foreground="White" FontFamily="Arial"  >
<Paragraph x:Name="para1″ FontSize="11″>

The following details have been obtained from Amazon to match your initial query. Some of the returned values may have been empty, so have been omitted from the results shown here. Also, where there have been more than one value returned via the Amazon Details, these too have been omitted for the sake of keeping things simple for this small demo application. Simple is good, when trying to show how something works.

XML
</Paragraph>
</FlowDocument>
</FlowDocumentPageViewer>

In C# Code Behind

C#
Paragraph paraHeader = new Paragraph();
paraHeader.FontSize = 12;
paraHeader.Foreground = headerBrsh;
paraHeader.FontWeight = FontWeights.Bold;
paraHeader.Inlines.Add(new Run("Paragraph Text"));
flowDoc.Blocks.Add(paraHeader);

Hyperlinks

In XAML

XML
<Paragraph FontSize="11″>
     <Hyperlink  Click="hl_Click" 
     NavigateUri="www.google.com">Click Here</Hyperlink>
</Paragraph>

In C# Code Behind

C#
Paragraph paraValue = new Paragraph();Hyperlink hl = new Hyperlink(new Run(
    "Click Here To View The Link Data"));
hl.FontSize = 11;hl.NavigateUri = new Uri(nonNullprop.PropertyValue);
hl.Click += new RoutedEventHandler(hl_Click);paraValue.Inlines.Add(hl);
flowDoc.Blocks.Add(paraValue);

Embedding UI Elements

In XAML

XML
<BlockUIContainer>
    <Button Width="60″ Height="60″ Click="Button_Click">
        Click me
    </Button>
</BlockUIContainer>

In C# Code Behind

C#
BlockUIContainer uiCont = new BlockUIContainer();
Button b = new Button();
b.Width = b.Height = 60;
b.Click += new RoutedEventHandler(Button_Click);
b.Content = "Click me";
flowDoc.Blocks.Add(uiCont);

Of course, this is only touching the surface of what can be done with FlowDocuments. But it gives you an idea of how flexible the formatting of documents is with WPF.

The screen shot below shows an example with some Paragraphs/Tables/Hyperlinks and UIElements in place.

37368/flowdocumentexample.png

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
Frank R. Haugen7-Aug-15 1:55
professionalFrank R. Haugen7-Aug-15 1:55 
GeneralRe: My vote of 5 Pin
Sacha Barber11-Aug-15 0:21
Sacha Barber11-Aug-15 0:21 
GeneralMy vote of 5 Pin
Koopakiller2-Feb-12 9:00
Koopakiller2-Feb-12 9:00 
GeneralMy vote of 5 Pin
ShlomiO9-Dec-11 1:49
ShlomiO9-Dec-11 1:49 
GeneralLet's Say Thanks Pin
mikefried21-Oct-10 0:02
mikefried21-Oct-10 0:02 
GeneralRe: Let's Say Thanks Pin
Sacha Barber21-Oct-10 1:24
Sacha Barber21-Oct-10 1:24 
GeneralMy vote of 4 Pin
DanishAbbas15-Oct-10 0:27
DanishAbbas15-Oct-10 0:27 
GeneralCool Sasha! Flowdocuments seem to be the elephant in the room Pin
Tawani Anyangwe17-Jun-09 9:21
Tawani Anyangwe17-Jun-09 9:21 
I'm glad you started this series. Flow documents seem to be the elephant in the room nobody want to talk about.
May I suggest a topic:
- Efficiently converting DataTable's / DataReader's to Flow document tables. (custom DataBinding or XML to XAML transformation etc.)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.