none
how to make page navigation from menu item click event in wpf?

    Question

  • hi..

    i use simple menu control in user control..... i register that user control in Generic.xaml for master page like functionality

    in menu item click event i need to make code to navigate to some another page i made code like following:

    <Grid>
    <Menu  Name="MainMenu" FontFamily="Tahoma" FontSize="14" Background="#47A4D7" Height="30">
          <MenuItem Name="PL" Header="Project List" Foreground="#FFFFFF" Width="100" Click="PL_Click"></MenuItem>
    </Menu>
    </Grid>

    in code side in PL_Click what i need to write to navigate abc.xaml....pls tell me it's tooo urgent



    Monday, May 26, 2008 11:31 AM

Answers

  • NavigationService.GetNavigationService(DependencyObject) returns the NavigationService of the navigation container the element is hosted in. Do you want the navigator that the menu is hosted in to navigate to Page1? If so, you can cast sender (should be the MenuItem) to DependencyObject and use that as the parameter.

     

    • Marked as answer by Marco Zhou Monday, June 02, 2008 3:35 AM
    Thursday, May 29, 2008 10:02 PM
  • I got the answer...

    private void PL_Click(object sender, RoutedEventArgs e)
            {
                Page1 h1 = new Page1();
                NavigationService.GetNavigationService(this).Navigate(h1)
               
             
            }

    this code working success fully

    thanks for ur replies

    soso
    • Marked as answer by jayma Tuesday, June 03, 2008 11:55 AM
    Tuesday, June 03, 2008 11:55 AM

All replies

  • What do you mean "navigate abc.xaml"?
    Monday, May 26, 2008 3:26 PM
  • It means i m at Home.xaml page on menu item click event i need to go to another page abc.xaml.... on menu item click event i need to go some anothe page but here NavigationService.Navigate code is not working...what is another option... pls tell me it's toooo urgent
    Tuesday, May 27, 2008 5:20 AM
  • but here NavigationService.Navigate code is not working.

    Could you please drop a sample to demonstrate how it doesn't work for you?

    Thanks
    Thursday, May 29, 2008 6:32 AM
  • THANKS..

    i add my menu control in one user control, like following

    <UserControl x:Class="abc.MenuMaster"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <Grid Height="30">
               
              
                <Menu  Name="MainMenu" FontFamily="Tahoma" FontSize="14" Background="#47A4D7" Height="30">
                    <MenuItem Name="PL" Header="PL" Foreground="#FFFFFF" Width="100" Click="PL_Click"></MenuItem>
                    <MenuItem Name="AP" Header="AP" Foreground="#FFFFFF" Width="100" Click="AP_Click"></MenuItem>
                   
    </Menu>
                   
            </Grid>
        </Grid>
    </UserControl>

    in code side in PL_Click event option like NavigationService.Navigate is not coming but in place of that that

    NavigationService.GetNavigationService(Dependancy object) only option arise... so how can i do page navigation

    e.g 
    private void PL_Click(object sender, RoutedEventArgs e)
            {
                Page1 h1 = new Page1();
                NavigationService.GetNavigationService(.........????)
               
             
            }

    pls reply fast.....it's urgent

    Thursday, May 29, 2008 9:56 AM
  • NavigationService.GetNavigationService(DependencyObject) returns the NavigationService of the navigation container the element is hosted in. Do you want the navigator that the menu is hosted in to navigate to Page1? If so, you can cast sender (should be the MenuItem) to DependencyObject and use that as the parameter.

     

    • Marked as answer by Marco Zhou Monday, June 02, 2008 3:35 AM
    Thursday, May 29, 2008 10:02 PM
  • I got the answer...

    private void PL_Click(object sender, RoutedEventArgs e)
            {
                Page1 h1 = new Page1();
                NavigationService.GetNavigationService(this).Navigate(h1)
               
             
            }

    this code working success fully

    thanks for ur replies

    soso
    • Marked as answer by jayma Tuesday, June 03, 2008 11:55 AM
    Tuesday, June 03, 2008 11:55 AM
  • Hey all this is hemant, m also struggling with the same problem n examle given above is not working for me!!!!!

    Thanks!!! :)



    • Proposed as answer by Samson Kassa Thursday, July 07, 2011 1:10 PM
    • Unproposed as answer by Samson Kassa Thursday, July 07, 2011 1:11 PM
    Wednesday, June 15, 2011 11:31 AM
  • The example given above is wrong. Here is what you must do:

    private void PL_Click(object sender, RoutedEventArgs e)
    {
    Page1 h1 = new Page1();
    h1.Show();  // or h1.Showdialog();            
              
            }

    Thursday, July 07, 2011 1:15 PM
  • Hi Jayma,

    While using ur code, am facing a null exception.

    Please help me out...

    Thanks.

    Sunday, December 25, 2011 8:03 AM