using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; using Microsoft.Practices.Prism.Regions; namespace DTS.Common { public class ViewerStackPanelRegionAdapter : RegionAdapterBase { public ViewerStackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory) { } protected override void Adapt(IRegion region, StackPanel regionTarget) { if (region == null) return; region.Views.CollectionChanged += (sender, e) => { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (UIElement element in e.NewItems) { regionTarget.Children.Add(element); } break; case NotifyCollectionChangedAction.Remove: foreach (UIElement elementLoopVariable in e.OldItems) { var element = elementLoopVariable; if (regionTarget.Children.Contains(element)) { regionTarget.Children.Remove(element); } } break; } }; } protected override IRegion CreateRegion() { return new AllActiveRegion(); } } }