I decided to use the RegionContext to propagate the IsActive state within the region.
Set it up as:
Regions:RegionManager.RegionContext="{Binding Path=IsActive, Mode=TwoWay}"
on my tab view (which is IActiveAware). Then in the child view I can listen to changes:
RegionContext.GetObservableContext((DependencyObject)View).PropertyChanged += new PropertyChangedEventHandler(VehiclesPresentationModel_PropertyChanged);
private void VehiclesPresentationModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Value")
{
IsActive = (bool)RegionContext.GetObservableContext((DependencyObject)View).Value;
}
}
The remaining issue was that the reverse would work. Setting IsActive on the tab view doesn't active the tab :(
I added a custom behavior and now it works. The custom bahavior is like:
public class RegionReverseActiveAwareBehavior : RegionBehavior
{
public const string BehaviorKey = "RegionReverseActiveAwareBehavior";
protected override void OnAttach()
{
Region.Views.CollectionChanged += new NotifyCollectionChangedEventHandler(Views_CollectionChanged);
}
private void Views_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (var item in e.NewItems)
{
IActiveAware activeAwareItem = item as IActiveAware;
if (activeAwareItem != null)
{
activeAwareItem.IsActiveChanged += new EventHandler(activeAwareItem_IsActiveChanged);
}
}
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (var item in e.OldItems)
{
IActiveAware activeAwareItem = item as IActiveAware;
if (activeAwareItem != null)
{
activeAwareItem.IsActiveChanged -= new EventHandler(activeAwareItem_IsActiveChanged);
}
}
}
}
private void activeAwareItem_IsActiveChanged(object sender, EventArgs e)
{
IActiveAware activeAware = sender as IActiveAware;
if (activeAware != null &&
activeAware.IsActive)
{
Region.Activate(activeAware);
}
}
}
And then I set it up on the TabControl
with:
RegionManager.GetObservableRegion(tabRegion).PropertyChanged +=
(sender, args) =>
{
if (args.PropertyName == "Value")
{
IRegion region = RegionManager.GetObservableRegion(tabRegion).Value;
region.Behaviors.Add(RegionReverseActiveAwareBehavior.BehaviorKey, new RegionReverseActiveAwareBehavior());
}
};
Hope that solves someone else's issue. Or maybe there's an easier way that I'm missing.