Navigating to a specific fragment in a flow document from code behind

╄→гoц情女王★ 提交于 2019-12-13 03:32:45

问题


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


回答1:


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




回答2:


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);


来源:https://stackoverflow.com/questions/2180517/navigating-to-a-specific-fragment-in-a-flow-document-from-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!