Reputation: 324
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;
}
}
}`
Upvotes: 0
Views: 1800
Reputation: 1
I know this is a fairly old topic, but this was my simple solution to this issue...
public class HtaTabControl : TabControl
{
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
var nc = new NavigationContext(null,
e.AddedItems.Count > 0 ? new Uri(e.AddedItems[0].GetType().Name, UriKind.RelativeOrAbsolute) : null);
if (e.RemovedItems.Count > 0)
{
if (e.AddedItems[0] is FrameworkElement fromTabItem)
{
var vm = fromTabItem.DataContext as INavigationAware;
vm?.OnNavigatedFrom(nc);
}
}
if (e.AddedItems.Count > 0)
{
if (e.AddedItems[0] is FrameworkElement toTabItem)
{
var vm = toTabItem.DataContext as INavigationAware;
vm?.OnNavigatedTo(nc);
}
}
base.OnSelectionChanged(e);
}
}
I'll probably revisit this at some point, but this works for me for now, so I can pass parameters between VMs when "Navigation" occurs.
Upvotes: 0
Reputation: 28
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:
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.
Upvotes: 1