How to Navigate using Frame (Window to Page) WPF in C#
Monday, December 19, 2011
1. Open you Visual Studio 2010
2. Select File, New, Project and Select WPF Application
And your project will look like this. |
4. Go to Toolbox (On the left side your) add Frame and Button
1 frame and 1 button. |
5. Right click WpfApplication (your projects title) on right side of the project
Click Add Then choose Page
6. Choose Page(WPF)
Name your Page.
Then Click Add
7. Add something or anything on your page.
Then go back to your MainWindow
8. Add this code on your XAML for Navigation
<Window.CommandBindings>
<CommandBinding Command="NavigationCommands.GoToPage" Executed="GoToPageExecuteHandler" CanExecute="GoToPageCanExecuteHandler" />
</Window.CommandBindings>
9. Double click the button and add this code just after the public MainWindow()
private void GoToPageExecuteHandler(object sender, ExecutedRoutedEventArgs e)
{
frame1.NavigationService.Navigate(new Uri((string)e.Parameter, UriKind.Relative));
}
private void GoToPageCanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
Type GoToPage to Command
Page1.xaml (or the name of your page where you want to navigate) to CommandParameter
And that's it!
Now, press F5 to run your program.
Running program. After clicking the Go To Page 1 button. |
0 comments:
Post a Comment