Question

I'm working on a WPF application based on the Microsoft Prism framework. One aspect of the application uses an ItemsControl region. I can add views to the ItemsControl by RequestNavigate-ing a view to the region. However, I don't see a way to RequestNavigate away from the region, i.e. so that the view is removed from the region and the "OnNavigatedFrom" callback is fired in the view's ViewModel.

The only way I've seen to remove a view from an ItemsControl region is to manually remove the view from the region like so:

IRegion myRegion = _regionManager.Regions["MyRegion"];
myRegion.Remove(viewToRemove);

This won't work for me, unfortunately, because I need the OnNavigatedTo and OnNavigatedFrom callbacks to be fired when the view is added to/removed from the region.

Am I missing something? Is there a way to RequestNavigate a view away from a region?

Was it helpful?

Solution

Based on my understanding, you would not need to execute OnNavigatedTo/From methods if you would not be navigating to a particular View.

Anyway, if you may need to execute the OnNavigatedFrom() implementation before removing the View from the ItemsControl, you could manually invoke it right before manually removing the View.

You could pass null to the NavigationContext argument if you would not need it, which I believe you don't, or you could manually create it otherwise. The solution would be as follows:

INavigationAware viewModel = viewToRemove.DataContext;
viewModel.OnNavigatedFrom(null);
IRegion myRegion = _regionManager.Regions["MyRegion"];
myRegion.Remove(viewToRemove);

I hope this helped you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top