How to cancel changing active window?

Docking/MDI for WPF Forum

Posted 9 months ago by Yuki
Version: 23.1.2
Avatar

Hellow,

I'd like to cancel changing active window.

Would you please teach me how to do it? (I'm using DockSite with MVVM pattern)

I have tried to change "MVVM DocumentWindows" sample as the following.

However, it does not work properly.

<Change point of sample program>

- Add `CanDocumentWindowsFloat="True"` to dockSite in MainView.xaml.

- Add below override method in TextDocumentItemViewModel.cs.

        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsActive" && Title == "Document1.txt")
            {
                if (!IsActive)
                {
                    var choice = MessageBox.Show("Do you want to switch active window?", Title, MessageBoxButton.YesNo);
                    if (choice == MessageBoxResult.Yes)
                    {
                        base.OnPropertyChanged(e);
                        return;
                    }

                    Application.Current.Dispatcher.BeginInvoke(() =>
                    {
                        IsActive = true;
                    });
                }
                else
                {
                    base.OnPropertyChanged(e);
                }
            }
            else
            {
                base.OnPropertyChanged(e);
            }
        }
    }

<Result>

- When I change active window to Document2.txt from Document1.txt and select "Yes" on messagebox, messagebox is shown again and window of Document2.txt is displayed as floating.

Best regards,

[Modified 9 months ago]

Comments (1)

Answer - Posted 9 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I think your code above had a couple issues in it. 

1) When the IsActive property was being set to true, you didn't call OnPropertyChanged which prevented some bindings from working properly.

2) If a MessageBox is shown in the middle of a tab being clicked, the WPF mouse capture of the tab is still active and the tab will think you drag/dropped when it returns from the Win32 MessageBox display.  Therefore, you need to dispatch the entire Win32 MessageBox display too.

I think this code change gets it all working how you want:

protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
	if (e.PropertyName == "IsActive" && Title == "Document1.txt") {
		if (!IsActive) {
			Application.Current.Dispatcher.BeginInvoke(() => {
				var choice = MessageBox.Show("Do you want to switch active window?", Title, MessageBoxButton.YesNo);
				if (choice == MessageBoxResult.Yes) {
					base.OnPropertyChanged(e);
					return;
				}

				IsActive = true;
			}, System.Windows.Threading.DispatcherPriority.Loaded);
		}
	}
			
	base.OnPropertyChanged(e);
}


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.