Files
DP44/enriched-partialglm/DTS Viewer/DTS.Viewer/RegionAdapters.md
2026-04-17 14:55:32 -04:00

67 lines
3.5 KiB
Markdown

---
source_files:
- DTS Viewer/DTS.Viewer/RegionAdapters/StackPanelRegionAdapter.cs
generated_at: "2026-04-16T11:23:32.925488+00:00"
model: "zai-org/GLM-5-FP8"
schema_version: 1
sha256: "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
```csharp
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
```
Initializes a new instance of the adapter, passing the region behavior factory to the base class.
#### Protected Methods
```csharp
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`: Iterates `e.NewItems` and adds each `UIElement` to `regionTarget.Children`.
- On `NotifyCollectionChangedAction.Remove`: Iterates `e.OldItems` and removes each `UIElement` from `regionTarget.Children` if present.
- Returns immediately if `region` is `null`.
```csharp
protected override IRegion CreateRegion()
```
Creates and returns a new `AllActiveRegion` instance, indicating all views in this region are considered active simultaneously.
## 3. Invariants
- The `region` parameter in `Adapt()` must not be `null` for any synchronization to occur (method returns early otherwise).
- All items in `region.Views` must be castable to `UIElement`; the code performs a direct cast without type checking.
- The `regionTarget` must be a valid `StackPanel` instance.
- 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 `StackPanel` controls.
## 5. Gotchas
- **Unhandled collection actions**: The `Adapt` method only handles `Add` and `Remove` actions. `Replace`, `Move`, and `Reset` actions from `NotifyCollectionChangedAction` are silently ignored, which could lead to desynchronization if the region's view collection triggers these actions.
- **No event unsubscription**: The `CollectionChanged` handler is never unsubscribed. If the adapter or region outlives the `StackPanel`, this could cause memory leaks or unexpected behavior.
- **Redundant variable assignment**: In the `Remove` case, `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.NewItems` and `e.OldItems` elements directly to `UIElement` without validation. Non-`UIElement` items will throw `InvalidCastException` at runtime.