0
votes

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?

1
What would happen if you could navigate away in a Region? Would you want the Region to have no Active Views or would you like the previous View become active? The main question would be why you don't want to Navigate to other View. Regards. - GOstrowsky
@GOstrowsky From my understanding, all views currently in the region have their IsActive property set to true. So ideally a RequestNavigateFrom method would remove the view from the region, set IsActive to false, and fire the OnNavigatedFrom callback. - Nathan Friend
Also, I'm not sure what you mean by "why you don't want to Navigate other View." Navigating to another view in an ItemsControl region just adds the view to the region; it doesn't remove or replace an existing view. - Nathan Friend

1 Answers

0
votes

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.