0

I have a WPF page used as an input form which contains a number of controls on one side, and a flow document reader on the other.

I want to set the content of this document reader to a specific part of a flow document which is loaded when the form is loaded, (via a loaded event).

I have found an article explaining how to do this, using fragments, but the examples shown are only expressed in XAML.

In my case I need to update the document property of the flow document reader when the user gives focus to one the controls (I have wired up the events already) therefore I need to do this in the code behind rather than XAML.

I have tried setting the document property to:

Document#Control_Sport

where Document is the name of XAML flow document and Control_Sport is the name of the fragment I need to navigate to.

However this gives an error, it doesn't like the hash sign being there.

I tried looking on MSDN but its XAML only. Is there a way I can do this through code?

Any help would be appreciated.

Felix,

Link to MSDN article: http://msdn.microsoft.com/en-us/library/ms750478.aspx#FragmentNavigation

fenfe1
  • 71
  • 1
  • 6

2 Answers2

1

First, create a Frame object inside your Page or Window object. Setting the JournalOwnership property to "OwnsJournal" will give the document its own navigation bar (forward and back arrows plus a history). You will probably need to add additional parameters to size and locate the frame within your document as well, but I didn't include them in my example since I don't know what your app requires:

<Frame Name="MyFrame" JournalOwnership="OwnsJournal" />

Then, create a pack URI for your document fragment. This document is assumed to be in the same directory as the application's executable; you will need to add more to the path to navigate to the directory where the document resides in your project:

Uri MyUri = new Uri("pack://application:,,,/MyXamlDocument.xaml#MyFragment");

Then, navigate to it from inside your button's Click handler or whatever other means you like to initiate the navigation:

MyFrame.Navigate(MyUri);
Evan
  • 825
  • 4
  • 14
Mark
  • 61
  • 1
  • 3
1

You can navigate to any Block within a FlowDocument by calling Block.BringIntoView.

Matthew Olenik
  • 3,577
  • 1
  • 28
  • 31
  • How exactly do I do this? Could you give an example, I found block as a property of the flowreader, flowreader.document and of the flowdocument used in the flowreader. thanks, felix – fenfe1 Feb 02 '10 at 19:16
  • The elements you add to FlowDocument are of type Block. Things like Paragraphs, Sections etc. You can call BringIntoView on any block element to make it show in a FlowDocumentReader, FlowDocumentPageViewer, etc. – Matthew Olenik Feb 03 '10 at 00:38
  • Right the paragraphs in my flow document are all named as follows; Fixture: Sport Examples: Curling, Talking How do I then bring this section in view? Like this, FlowScrollReader.document = Document.BringIntoView.Control_Sport" Cause that doesn't work! Sorry to keep asking, I'm new to WPF and the examples microsoft give are either in XAML or completely useless. thanks, – fenfe1 Feb 03 '10 at 15:19
  • If you give the element a name: then it will generate a member of that name in the code which you can use. So in this case, sport.BringIntoView() – Matthew Olenik Feb 03 '10 at 17:30