1. Computing & Technology

Discuss in my forum

FlowDocument - A New Kind of Object in WPF

Using Figures and Floaters

By , About.com Guide

See More About:

Figure and Floater can be used to put other objects into your FlowDocument. Figure is used for content that stands by itself. (I used a picture and Figure in the example below.) Floater will put content that is integrated into your text, but separate from it, such as a few words from a quote in the text that you want to stay with the entire quote. Putting images into WPF documents can be a lot of fun too, especially if you do it in VB.NET code rather than XAML, so I used an example that places a picture of Sirius the dog star into an anchored position with respect to the text.

Private Sub Window1_Loaded( _
   ByVal sender As System.Object, _
   ByVal e As System.Windows.RoutedEventArgs) _
   Handles MyBase.Loaded
   Dim aParagraph As Paragraph = New Paragraph
   Dim aFigure As Figure = New Figure()
   aFigure.Width = New FigureLength(200)
   aFigure.Height = New FigureLength(200)
   aFigure.Background = Brushes.DeepPink
   aFigure.VerticalAnchor = FigureVerticalAnchor.ParagraphTop
   Dim aImage As Image = New Image
   Dim aBitmapImage As New BitmapImage
   aBitmapImage.BeginInit()
   aBitmapImage.UriSource = New Uri("C:\...\sirius.JPG")
   aBitmapImage.DecodePixelWidth = 200
   aBitmapImage.EndInit()
   aImage.Source = aBitmapImage
   aFigure.Blocks.Add(New BlockUIContainer(aImage))
   Dim aSpan As Span = New Span
   aSpan.Inlines.Add(aFigure)
   aParagraph.Inlines.Add(aSpan)
   aParagraph.Inlines.Add(New Run("Lorem ipsum ... etc.
   Dim aFlowDocument As FlowDocument = New FlowDocument
   aFlowDocument.Blocks.Add(aParagraph)
   Dim aFlowDocumentReader As FlowDocumentReader _
      = New FlowDocumentReader
   aFlowDocumentReader.Document = aFlowDocument
   Window1.Content = aFlowDocumentReader
End Sub

Again, here's the result:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Like WPF itself, the possibilities here are endless. For example, not only do you have the Block types listed earlier available to you, but you can define your own Block type and put anything you can write code for into your FlowDocument. This article has just scratched the surface.

©2012 About.com. All rights reserved.

A part of The New York Times Company.