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



3. Click Ok
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;
        }



10. Right Click the button Go to page 1 and select Properties
Type GoToPage to Command
         Page1.xaml (or the name of your page where you want to navigate) to CommandParameter



And on CommandTarget click the box or the advanced properties, Apply Data Binding



Choose Element Name, frame1 (or the title of your frame)


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

About Me

Jemelyn
19 years old. Filipina. 4th year college. Computer Science student. Taylor Swift fan. Technology enthusiast. Frustrated blogger. Servant of God.
View my complete profile

  © Blogger template Brownium by Ourblogtemplates.com 2009

Back to TOP