Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

Docking: the host hands its data context to the content it re-parents

Alexander Graef020f8849e145
diff --git a/CLAUDE.md b/CLAUDE.md
index 73b3edd..e1b3802 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -384,6 +384,15 @@ WPF shell as it stands:
- **Unsaved edits** (2026-10-14): `MainWindow.IsDirty`, set by the watcher on any model change, cleared on
open/new/save; the title gets `*` after the file (or "Untitled*", `TitleConverter`, re-read on a language
switch) and New, Open, Close, Exit and a drop ask Yes/No/Cancel (`ConfirmDiscard`, `SaveChangesFormat`).
+- **The dock host re-parents its content** when a pane opens or closes (`DockHost.Rebuild` clears and
+ rebuilds its tree); out of the tree a content's inherited `DataContext` would be gone and its bindings
+ would pass through null and back (the preview read that as a new project and a new run and went to
+ frame 0, and the project tree dropped its selection: an item source going null makes a `TreeView` select
+ nothing, 2026-10-15). The host hands its own data context to every hosted content without one of its own
+ as a local value (`HandOverDataContext`, on its own data context change, on items added, and before each
+ rebuild), so the content never depends on the tree it is moved around in; the panes need no guard. A
+ first version pinned and released the context around the rebuild, and released it before the new
+ group containers had applied their templates, which was exactly the null it meant to prevent.
- Preview document: `Panes/PreviewPane`. Two pictures (field,
intensity) side by side or stacked (`Orientation`, persisted as the `PreviewOrientation` user
setting), each centred in its cell with its label laid out by `Controls/PictureFrame` (label above
diff --git a/WaveDude.WPF/Docking/DockHost.cs b/WaveDude.WPF/Docking/DockHost.cs
index cea87c6..354cea4 100644
--- a/WaveDude.WPF/Docking/DockHost.cs
+++ b/WaveDude.WPF/Docking/DockHost.cs
@@ -52,6 +52,12 @@ public sealed class DockHost : Grid
}
});
DragManager = new DockDragManager(this);
+ // the hosted content is re-parented on every rebuild and leaves the tree for a moment, so it gets the
+ // host's data context handed over explicitly rather than inherited (2026-10-15): inherited, its
+ // bindings passed through null and back, which put the preview at frame 0 and made the project tree
+ // drop its selection (an ItemsSource going null makes a TreeView select nothing)
+ DataContextChanged += (_, _) => HandOverDataContext();
+ Items.CollectionChanged += (_, _) => HandOverDataContext();
ToggleCommand = new RelayCommand<DockItem>(item =>
{
if (item.Group != null)
@@ -623,6 +629,7 @@ public sealed class DockHost : Grid
private void Rebuild()
{
+ HandOverDataContext();
foreach (DockGroupControl control in _groupControls)
{
control.Detach();
@@ -646,6 +653,30 @@ public sealed class DockHost : Grid
LayoutChanged?.Invoke(this, EventArgs.Empty);
}
+ /// <summary>The hosted contents whose data context the host set, so a change of the host's reaches them and one of their own is left alone.</summary>
+ private readonly HashSet<FrameworkElement> _handedOver = [];
+
+ /// <summary>
+ /// Sets the host's data context on every hosted content (the items' and the document) that has none of its
+ /// own, as a local value that survives the re-parenting of a rebuild; a content with its own stays untouched.
+ /// </summary>
+ private void HandOverDataContext()
+ {
+ object? context = DataContext;
+ IEnumerable<FrameworkElement> contents = Items.Select(item => item.Content as FrameworkElement).Append(_center).OfType<FrameworkElement>();
+ foreach (FrameworkElement element in contents)
+ {
+ if (_handedOver.Contains(element) || element.ReadLocalValue(DataContextProperty) == DependencyProperty.UnsetValue)
+ {
+ if (context != null)
+ {
+ element.DataContext = context;
+ _handedOver.Add(element);
+ }
+ }
+ }
+ }
+
private FrameworkElement Build(DockNode node)
{
FrameworkElement element;