Search code examples
wpfprism

OnNavigatedTo is not called in TabControl when selecting tab when using PRISM regions


I have registered my views for the TabControl with Region manager and views are shown properly when tab is selected.

The problem is that when I select new tab item OnNavigatedTo is not called for that view or its view model.

I'm using PRISM 6.3

UPDATE

ViewModel

`public class ValuationViewModel : IViewModel, INavigationAware { private IRegionManager _regionManager;

    public string Title { get; set; }

    public ValuationViewModel(IRegionManager regionManager)
    {
        Title = "PERFORM VALUATION";

        _regionManager = regionManager;
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }
}`

View `public partial class ValuationView : UserControl, IView { private IRegionManager _regionManager;

    public ValuationView(ValuationViewModel viewModel)
    {
        InitializeComponent();

        ViewModel = viewModel;
    }

    public IViewModel ViewModel
    {
        get
        {
            return (IViewModel)DataContext;
        }
        set
        {
            DataContext = value;
        }
    }
}`

Solution

  • Without code, nobody can give you the correct answer. Its probably the best, if you show us your ViewModel for your "TabItem" View.

    Assuming you registered your view and set your ViewModel in DataContext correctly, it could be possible that forget just a simple thing.

    To manage your problem make sure you implemented the following things correctly:

    1. Create a region for your TabControl
    2. Register your view in that region
    3. Make sure the DataContext is correctly set to your ViewModel
    4. Make sure your ViewModel implemented INavigationAware

    Update 1:

    After testing a lot I found a simple answer unfortunately:

    Members of INavigationAware (OnNavigatedTo, IsNavigationTarget & OnNavigatedFrom) are called when the NavigationService is navigating. They aren't if you click on the TabItemHeader.

    To solve your problem you have several options. One option is to start a navigation request when the user click on the TabItemHeader ( bad approach).

    In my opinion you should use the IActiveAware Interface ( https://msdn.microsoft.com/en-us/library/microsoft.practices.prism.iactiveaware(v=pandp.50).aspx).

    It will solve your problem, because the navigation via RegionManager and the clicking on the TabItemHeader results in the same: INavigationAware.IsActive = true.

    Now you are able to detect when your tab is shown or not and react.