Let us say, you have a scenario to navigate from user control. In the code behind of the user control, very first put the below namespace, using System.Windows.Navigation; Next globally define an event as below, public event EventHandler EventForPageNavigation; After this you need to create a method to handle the page navigation event as below. This function is taking URI to navigate as input parameter. Let us say you have a button on the user control and you want to navigate on click event of that button then you need to call above function inside the click event of the button like below:Finally the code behind of the user control should look like below: using System; using System.Windows.Controls; using System.Windows.Navigation; namespace test { public partial class MyUserControl : UserControl { public event EventHandler EventForPageNavigation; public MyUserControl() { InitializeComponent(); } public void MethodToNavigateToPage(Uri uri) { var e = new NavigationEventArgs(null, uri); if (EventForPageNavigation != null) EventForPageNavigation(this, e); } private void btnNavigate_Click(object sender, System.Windows.RoutedEventArgs e) { MethodToNavigateToPage(new Uri("/ImageUpload.xaml", UriKind.Relative)); } } } Next you need to handle this event of the main page where user control is being used. Let us say you are using user control with the name NavigationUserControl. What I mean while adding user control on the XAML of the main page you gave the name NavigationUserControl. First you will have registered the event in the OnNavigatedTo method or constructor of page as below,
NavigationUserControl.EventForPageNavigation += new EventHandler(NavigationUserControl _NavigateToPageEvent);
Then in the navigated event, navigate to the URI as below,
void NavigationUserControl_NavigateToPageEvent(object sender, EventArgs e) { NavigationService.Navigate(((NavigationEventArgs)e).Uri); } So in this way you can perform navigation in a user control. I hope this post is useful. Thanks for reading
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: