3.5 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T11:23:32.925488+00:00 | zai-org/GLM-5-FP8 | 1 | d99a52248dae627a |
StackPanelRegionAdapter Documentation
1. Purpose
StackPanelRegionAdapter is a Prism region adapter that enables a WPF StackPanel control to act as a Prism region. It bridges the Prism region infrastructure with the StackPanel's Children collection, automatically synchronizing views added to or removed from the region with the panel's visual children. This allows views to be dynamically composed within a StackPanel using Prism's region management system.
2. Public Interface
StackPanelRegionAdapter (Class)
Inherits from: RegionAdapterBase<StackPanel>
Constructor
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
Initializes a new instance of the adapter, passing the region behavior factory to the base class.
Protected Methods
protected override void Adapt(IRegion region, StackPanel regionTarget)
Subscribes to the region.Views.CollectionChanged event and synchronizes the StackPanel's Children collection:
- On
NotifyCollectionChangedAction.Add: Iteratese.NewItemsand adds eachUIElementtoregionTarget.Children. - On
NotifyCollectionChangedAction.Remove: Iteratese.OldItemsand removes eachUIElementfromregionTarget.Childrenif present. - Returns immediately if
regionisnull.
protected override IRegion CreateRegion()
Creates and returns a new AllActiveRegion instance, indicating all views in this region are considered active simultaneously.
3. Invariants
- The
regionparameter inAdapt()must not benullfor any synchronization to occur (method returns early otherwise). - All items in
region.Viewsmust be castable toUIElement; the code performs a direct cast without type checking. - The
regionTargetmust be a validStackPanelinstance. - The adapter creates an
AllActiveRegion, meaning all views added to this region will be active at once (as opposed to single-active regions like tabs).
4. Dependencies
This module depends on:
System.Collections.Specialized(NotifyCollectionChangedAction)System.Windows(UIElement)System.Windows.Controls(StackPanel)Microsoft.Practices.Prism.Regions(IRegion,IRegionBehaviorFactory,RegionAdapterBase<T>,AllActiveRegion)
Consumers:
- This adapter must be registered with the Prism region adapter mappings (not shown in source) to be used for
StackPanelcontrols.
5. Gotchas
- Unhandled collection actions: The
Adaptmethod only handlesAddandRemoveactions.Replace,Move, andResetactions fromNotifyCollectionChangedActionare silently ignored, which could lead to desynchronization if the region's view collection triggers these actions. - No event unsubscription: The
CollectionChangedhandler is never unsubscribed. If the adapter or region outlives theStackPanel, this could cause memory leaks or unexpected behavior. - Redundant variable assignment: In the
Removecase,var element = elementLoopVariable;creates an unnecessary local copy. This appears to be a legacy closure pattern but serves no purpose here since the variable is not captured. - No type safety on collection items: The code casts
e.NewItemsande.OldItemselements directly toUIElementwithout validation. Non-UIElementitems will throwInvalidCastExceptionat runtime.