What is preferred way to remove a DockItem from ObservableCollection binded to DockingManager?

Hi,

I saw the documentation to bind the collection dock items to the docking manager here.

Data Binding in WPF Docking control | Syncfusion

But when I close the window, I want the instance to also be removed, because the items in this collection can be instantiated many times. However, when I look at the collection it is still there.

What is the preferred way to remove from the ObservableCollection when the window is closed so that the dock item can be cleaned up?


Is it to handle WindowClosing event?

If so, how to access the DockItem in this case? 




3 Replies 1 reply marked as answer

GT Gokul Thanudhas Syncfusion Team April 25, 2022 11:45 AM UTC

Hi Lorenzo,


The state of the window will be moved to hidden state while closing the window. To remove the DockItem from ObservableCollection remove the item from the Children of DockingManager in StateChanged event.


Code Snippets


private void docking_DockStateChanged(FrameworkElement sender, DockStateEventArgs e)

        {

            if (e.NewState == DockState.Hidden)


                docking.Children.Remove(sender);


        }


Regards,

Gokul T.



LO Lorenzo April 25, 2022 01:04 PM UTC

Thanks Gokul,

What is the best way to get a reference to the DockItem and/or the parent dockingManager?

I need to know the window I'm working with to determine if it should be removed or just stay hidden.



GT Gokul Thanudhas Syncfusion Team April 26, 2022 02:33 PM UTC

Hi Lorenzo,


The ActiveWindow property will store the window that you are currently working on. Comparing the name of the ActiveWindow with the name of the items in Itemsource of DockingManager you can able to get the DockItem. Please refer the attached sample for your reference


Regards,

Gokul T.


Attachment: DockingManager_ActiveWindow_3140a46d.zip

Marked as answer
Loader.
Up arrow icon