WPF How to navigate from page to page for frame in code behind
-
Tuesday, August 11, 2009 9:33 PMIn my xaml page, i use <Button Command="NavigationCommands.GoToPage" CommandParameter="Page.xaml" CommandTarget="{Binding ElementName=frmContent}">to navigate from page to page for my Frame. This is working fine.But how do i do the samething in cs page? Please help. Thanks.
All Replies
-
Tuesday, August 11, 2009 9:47 PMHi dragonz1,
You need to get first the NavigationService and then Navigate:
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new System.Uri("Page.xaml", UriKind.RelativeOrAbsolute));
or:
Page1 page = new Page1();
nav.Navigate(page);
Hope it helps.
Regards,
Federico Benitez My blog -
Tuesday, August 11, 2009 10:09 PMThanks for your reply.Yes, actually i do have the NavigationService.in xaml.<Window.CommandBindings><CommandBinding Command="NavigationCommands.GoToPage" Executed="GoToPageExecuteHandler" CanExecute="GoToPageCanExecuteHandler" /></Window.CommandBindings>in my window.xmal.cs page,private void GoToPageExecuteHandler(object sender, ExecutedRoutedEventArgs e){frmContent.NavigationService.Navigate(new Uri((string)e.Parameter, UriKind.Relative));}private void GoToPageCanExecuteHandler(object sender, CanExecuteRoutedEventArgs e){e.CanExecute = true;}it's working if i create a button and use<Button Command="NavigationCommands.GoToPage" CommandParameter="Page.xaml" CommandTarget="{Binding ElementName=frmContent}">but i want to see if there is a way to do the same thing in cs page instead of create button for navigation. I'm trying to navigate for mouseClick event and other listview item selected event. Thanks.
-
Tuesday, August 11, 2009 10:36 PMHi dragonz1,
Sorry but I am a little slow :). I dont understand exactly what you are trying to achieve. You want to navigate from the codebehind or the XAML? You dont want to navigate with a button event?
Regards,
Federico Benitez My blog -
Tuesday, August 11, 2009 11:27 PMhi Federico,No, i'm not good at explaining.Yes, i still want to navigate with my button event, it's working fine with button, but let's say if i want to use Label instead. For label it doesn't have Command, CommandParameter and CommandTarget. So how can i do it?Thank you for your time.
-
Tuesday, August 11, 2009 11:38 PM
Hi dragonz1,
In the Label you cannot do it because it does not react to the mouse events, so by clicking on the Label you wont be able to raise an event. But if you want to do it on the ListView SelectionChanged event (as I think you do) here is the sample:
private
void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("Page.xaml", UriKind.RelativeOrAbsolute));
}
Let me know if it helps.
Regards,
Federico Benitez
My blog- Proposed As Answer by LiangTao Wednesday, August 12, 2009 12:40 AM
- Marked As Answer by Linda LiuModerator Wednesday, August 19, 2009 2:55 AM
-
Friday, April 13, 2012 12:20 PM
Dude,none of the two code is working for me... :(
God knows why..
its showing an error-
"Object reference not set to instance of an object"...
What am I suppose to do after this??

