WPF learning path (9) navigation link, wpf navigation link
Hyperlink
Hyperlink is a Hyperlink in WPF. In addition to navigation between pages, you can also navigate paragraphs under the same page.
Instance:
<Grid> <FlowDocumentReader> <FlowDocument> <Section LineHeight="25" FontSize="15"> <List> <ListItem> <Paragraph> <Hyperlink NavigateUri="CustomPage.xaml#first"> First Paragraph </Hyperlink> </Paragraph> </ListItem> <ListItem> <Paragraph> <Hyperlink NavigateUri="CustomPage.xaml#second"> Second Paragraph </Hyperlink> </Paragraph> </ListItem> </List> </Section> <Paragraph x:Name="first" FontSize="20" Background="AliceBlue"> 1. First paragraph content </Paragraph> <Paragraph x:Name="second" FontSize="20" Background="AliceBlue"> 2. Second paragraph content </Paragraph> <Paragraph> <Hyperlink NavigateUri="SimplePage.xaml" > Hello WPF </Hyperlink> </Paragraph> </FlowDocument> </FlowDocumentReader></Grid>
If the page boarding window is not NavigationWindow, the hyperlink cannot be navigated.
By coding and navigation
In some cases, Hyperlink cannot be implemented. It must be implemented through NavigationService.
1. values need to be passed in the navigation
2. Set properties before navigation to the page
3. You only need to know which page to navigate to when running
Instance:
DemoPage. xaml
<Page.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="15" /> <Setter Property="Margin" Value="5" /> </Style></Page.Resources><StackPanel> <TextBlock> Navigate to <Hyperlink x:Name="link1" Click="link_click"> SimplePage.xaml </Hyperlink> </TextBlock> <TextBlock> Navigate to <Hyperlink x:Name="link2" Click="link_click"> SimplePage.xaml </Hyperlink> (Call a constructor with parameter) </TextBlock> <TextBlock> Navigate to <Hyperlink x:Name="link3" Click="link_click"> .Net Object </Hyperlink> </TextBlock> <TextBlock> Navigate to site <Hyperlink x:Name="link4" Click="link_click"> http://www.bing.com </Hyperlink> </TextBlock></StackPanel>
class Person{ public string Name { get; set; } public int Age { get; set; } public override string ToString() { return "Name: " + Name + "\nAge: " + Age; }}
Page. xaml. cs
private void link_click(object sender, RoutedEventArgs e){ Hyperlink link = sender as Hyperlink; if (link == link1) { NavigationService.Navigate(new Uri("pack://application:,,,/SimplePage.xaml")); } else if (link == link2) { NavigationService.Navigate(new SimplePage("Hello Navigation")); }else if (link == link3) { NavigationService.Navigate(new Person() { Name = "Alex", Age = 25 }); } else if (link == link4) { NavigationService.Navigate(new Uri("http://www.bing.com")); }}
Other navigation methods
Navigation Toolbar
Navigation command
Add a Button to control navigation, which is equivalent to a custom navigation toolbar.
<Button Height="50" Width="100" Content="Back" Command="NavigationCommands.BrowseBack" /><Button Height="50" Width="100" Content="Forward" Command="NavigationCommands.BrowseForward" />
History
In WPF, Jaurnal records every navigation Operation to implement the navigation toolbar function.
Journal contains two data stacks to record the display status of the forward and backward pages. Each related Page corresponds to a JournalEntry. Automatic Log status recovery is only valid when you click the forward and backward buttons on the navigation bar.
To be continue...