Answered FlowDocument Paragraph and Images

  • Thursday, July 06, 2006 10:07 AM
     
     

    Thanks for taking the time to read this post.

    I might be completely lost here at the moment, but I ran into a very annyoing problem when inserting Inlines into a Paragraph in a FlowDocument.

    In C# I am trying to insert an Image object into a Figure, pass that Figure as an Inline to a Paragraph and then add the Paragraph to a FlowDocument.

    In XAML I do:

              <FlowDocument ColumnGap="5" ColumnWidth="150">
                <Paragraph>
                <Figure>
                  <Paragraph>
                    <Image Source="koizumi.jpg"/>
                  </Paragraph>
                </Figure>
                </Paragraph>
              </FlowDocument>

    But in C# when I do (short version):

    Image photo = new Image();
    BitmapImage photoFile = new BitmapImage();
    photoFile.BeginInit();
    photoFile.UriSource = new Uri(photourl, UriKind.Relative);
    photoFile.EndInit();
    photo.Source = photoFile;
    Paragraph pPhoto = new Paragraph(photo);

    I get an error that says:

    Error 2 Argument '1': cannot convert from 'System.Windows.Controls.Image' to 'System.Windows.Documents.Inline'

    Am I missing something completely obvious here? Lack of Sleep()?

    Would appreciate any input...

Answers

  • Thursday, July 06, 2006 12:54 PM
    Moderator
     
     Answered

    Paragraph's content property is Inlines.  Inlines is of type InlineCollection.

    The documentation for InlineCollection goes into some detail....
    InlineCollection can only hold objects of type Inline.  In order to put text or a UIElement inside, you need to use a method that "wraps" the text or UIElement with a <Run /> or <InlineUIContainer />.  Or you could create the Run or InlineUIContainer yourself.

    ...
    Paragraph pPhoto = new Paragraph();
    pPhoto.Inlines.Add(photo);  //This will end up creating a wrapper element
                                             //of type InlineUIContainer for the photo and then add
                                             //it to the Inlines collection.

     

    Searching the net for InlineCollection (the type of the Inlines property) also gives you these 2 articles.  They may be worth a read...

    Petzold Book Blog - XAML, Code, and TextBlock

    The crucial property of TextBlock is Inlines , which is an object of type InlineCollection , which is a collection of Inline objects. Here's a partial class hierarchy starting from Inline showing the ...

    www.charlespetzold.com/blog/0511201108.html
     

    Visiting Inlines in a Block? - MSDN Forums

    void VisitInlines(InlineCollection icoll) {   foreach(Inline item in icoll)   {     // ... do something to the inline     if(item is Span)     {       VisitInlines((item as Span).Inlines)

    forums.microsoft.com/MSDN/ShowPost.aspx?PostID=341326&SiteID=1
     
    Thx, Rob
    Rob Relyea | Program Manager | WPF Team
  • Thursday, July 06, 2006 1:43 PM
    Moderator
     
     Answered
    you could also use a blockUIContainer to add the image if inlines are not needed :)

All Replies

  • Thursday, July 06, 2006 12:54 PM
    Moderator
     
     Answered

    Paragraph's content property is Inlines.  Inlines is of type InlineCollection.

    The documentation for InlineCollection goes into some detail....
    InlineCollection can only hold objects of type Inline.  In order to put text or a UIElement inside, you need to use a method that "wraps" the text or UIElement with a <Run /> or <InlineUIContainer />.  Or you could create the Run or InlineUIContainer yourself.

    ...
    Paragraph pPhoto = new Paragraph();
    pPhoto.Inlines.Add(photo);  //This will end up creating a wrapper element
                                             //of type InlineUIContainer for the photo and then add
                                             //it to the Inlines collection.

     

    Searching the net for InlineCollection (the type of the Inlines property) also gives you these 2 articles.  They may be worth a read...

    Petzold Book Blog - XAML, Code, and TextBlock

    The crucial property of TextBlock is Inlines , which is an object of type InlineCollection , which is a collection of Inline objects. Here's a partial class hierarchy starting from Inline showing the ...

    www.charlespetzold.com/blog/0511201108.html
     

    Visiting Inlines in a Block? - MSDN Forums

    void VisitInlines(InlineCollection icoll) {   foreach(Inline item in icoll)   {     // ... do something to the inline     if(item is Span)     {       VisitInlines((item as Span).Inlines)

    forums.microsoft.com/MSDN/ShowPost.aspx?PostID=341326&SiteID=1
     
    Thx, Rob
    Rob Relyea | Program Manager | WPF Team
  • Thursday, July 06, 2006 1:43 PM
    Moderator
     
     Answered
    you could also use a blockUIContainer to add the image if inlines are not needed :)
  • Friday, July 07, 2006 12:30 AM
     
     

    thanks a lot people! I really appreciate it!

    Actually I just got back to work (in Tokyo so there is a certain time-difference).

    I ended up using the BlockUIContainer and then added that to the Figure, but I will def. try the Inline solution as well.. Are there any perf gains/losses in comparing the two? Specifically when it comes to resizing the FlowDocumentPageViewer host element?