ToolWindows state is lost when DataContext is updated

Docking/MDI for WPF Forum

Posted 1 year ago by Michael Janulaitis - Corner Bowl Software
Version: 22.1.4
Platform: .NET 6.0
Environment: Windows 11 (64-bit)
Avatar

I have a view which has a docksite attached.  When I change my DataContext the ToolWindows states are lost.  How can I retain the state between DataContext updates?  I have the following code which attempts to save the state when the docksite datacontext is changed.  The ToolWindows have the correct sizes however when  I serialize out the state the XML is incorrect and shows the initialized values.

Even if I trap the _dockSite.WindowsClosing += _dockSite_WindowsClosing event and try to serialize out the values are incorrect.  The only time the serialization is correct is when I fully unload the view, although I a haven't tired adding a callback from my View Model because I'd really like to avoid a callback reference from from my viewmodel to my code beind.

public void SaveDockState()
        {
            if (_dockSite == null)
                return;

            if (_dockSite.ToolWindows.Count == 0)
                return;

            if (LastTimeSaved != null && DateTime.Now - LastTimeSaved < TimeSpan.FromSeconds(1))
                return;

            string xml = s.SaveToString(_dockSite);

            XFile.StringToFile(xml, StateFilePath);

            LastTimeSaved = DateTime.Now;
        }

After trying out a slew of event handlers I think I have this working using the WindowsClosing and WindowsOpening along with a variable that checks to see if the the docksite is loading state so there isn't a recursion bug on load.

[Modified 1 year ago]

Comments (1)

Posted 1 year ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

You would need to serialize things before your DataContext actually changes, or else the layout is already modified at that point. 

Perhaps you could add some property to your view that you update as the view's DataContext updates to the new DataContext value?  When you detect a property value change on your new property, do your layout serialization, then set the DockSite.DataContext to the new value.  That way you delay the DockSite's DataContext change until after your serialization occurs, instead of inheriting the change too quickly.


Actipro Software Support

The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.