Question

Is there anyway to get the hostcontrol / parentcontrol from a region in PRISM C# WPF. Im trying to write an custom region behaviour that modifies the hostcontrol if there is no views in the region.

There is a HostControl property in RegionBehavior class but it is alwasy null. How do i set it ? or how to get the hostcontrol. Thanks for advice!

[Export(typeof(CollapseRegionBehavior))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class CollapseRegionBehavior : RegionBehavior, IHostAwareRegionBehavior
{

    public DependencyObject HostControl { get; set; }

    protected override void OnAttach()
    {
        if (this.Region.ActiveViews.Count() == 0)
        {

        }
    }
}
Was it helpful?

Solution

Are you adding the RegionBehavior manually or do you register it in the bootstrapper in ConfigureDefaultRegionBehaviors?

It seems that if you add a behavior manually like this:

IRegion region = regionManager.Regions["MyRegion"];
region.Behaviors.Add("MyBehaviorKey", new MyBehavior());

the IHostAwareRegionBehavior interface has no effect. If you look at the PRISM source code in RegionBehaviorCollection.cs (Prism.Dektop.Regions) you'll clearly see that the Add method does not do anything related to the IHostAwareRegionBehavior interface.

However in RegionAdapterBase.cs, the interface is checked for in AttachDefaultBehaviors.

I ended up registering my RegionBehavior in the bootstrapper and checking the region name to see if there is work to be performed. Not very clean, but it works.

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