Changing Close button functionallity

Docking/MDI for WPF Forum

Posted 9 years ago by Bret Naughton
Version: 10.1.0523
Avatar
Hi,

I am currently using the WindowClosing event with added code, to prompt/confirm from the user if the toolwindow should be closed or not. This works fine, however, depending on the toolwindow closed, I also need to close additional toolwindows related to the originally closed toolwindow.

If I try to add code to do this within the WindowClosing event, it gets a little complicated as I then have a routine that is trying to close windows from within the WindowClosing routine code - calling itself from within itself.

Wanted to know if there is a way to have the toolwindow 'x' close button only call a subroutine (and not trigger any toolwindow closing), which has all my code to close each of the required toolwindows.

Hope this makes sense.

Thanks

Comments (6)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Bret,

The close button uses the DockingCommands.CloseWindow routed command. We register a class handler on the DockingWindowContainer type, which either closes the selected tool window or all the associated tool windows (based on DockSite.ClosePerContainer setting).

The easiest solution would be to use a bool field to indicate whether you are currently executing in your WindowClosing handler. Something like:
private bool inWindowClosing = false;

private void OnWindowClosing(...) {
    if (inWindowClosing)
        return;
        
    inWindowClosing = true;
    try {
        // ... your code goes here ...
    }
    finally {
        inWindowClosing = false;
    }
}
You may run into an issue with this type of code, since the visual tree is changing while the original tool window is still closing. If so, you can register a preview handler for the DockingCommands.CloseWindow command and close all the tool windows you need to there. You should be able to add the preview handler directly on the DockSite. More information can be found here.


Actipro Software Support

Posted 9 years ago by Bret Naughton
Avatar
Hi, thanks for this... I am going to go with registering the preview handler, but do you have an example of this. I am having trouble adding the preview handler directly to the DockSite?

Thanks
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Bret,

There is an example on MSDN at the link provided. It uses AddExecutedHandler, but you can just change that to AddPreviewExecutedHandler. This can only be done from code-behind, not from XAML. So you will need to give the DockSite a name, then register the handler after InitializeComponent or whenever the DockSite is loaded.

In your handler, you will need to check the e.Command is equal to DockingCommands.CloseWindow.


Actipro Software Support

Posted 9 years ago by Bret Naughton
Avatar
Hi, Thanks for this. I have implemented OK except for one thing... in the preview routine, I seem to be able to identify the dockingcontainer and not the actual toolwindow that is attempting to be closed(i.e. the uniqueid of the toolwindow) using the e.source method. I can't use ActiveWindow (due to the close button on the toolwindow not activating the window). Please let me know if this is possible. I could do this on the closing event (see previous message trail) , as that allowed the use of e.window, but as I am running code to loop through and destroy appropriate toolwindows, the preview event is better suited. Thanks


Here are my lines of code referencing the preview event

Public Sub New()
InitializeComponent()
'
' rest of my code
'
CommandManager.AddPreviewExecutedHandler(dockSite, AddressOf PreviewEvent)
End Sub
.
.
.
.
Private Sub PreviewEvent(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
If e.Command Is DockingCommands.CloseWindow Then
' logic for deciding which toolwindows to close/destroy
msgbox(e.source)
End If
End Sub
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Bret,

Once you have the ToolWindowContainer, you can get the selected ToolWindow using the SelectedWindow property. That is what we use when handing the commands in our code.


Actipro Software Support

Posted 9 years ago by Bret Naughton
Avatar
Thanks, works great.
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.