Ability to show a non-modal WPF window #40
-
I recently updated an extension to use WPF instead of winforms for all things UI related. Previously if I needed to show a window I would simply call window.Show() or window.Showdialog() to open the window. However, when I open a non-modal window (window.Show()) then weird things start to happen... like the Backspace in a textbox causes the text in currently open file in Visual Studio to be edited instead of what's in the textbox. The same thing happens with the tab key. After doing some searching it appears that there isn't really a supported way to get this to work as VS intercepts key strokes or something for non-modal windows. I found documentation regarding modal windows here. I found this issue on StackOverflow that describes the exact same issue here. The "supported" way seems to be to create a toolwindow or document window... but my window is short lived and shouldn't be dockable etc. It really just needs to be a regular WPF window. Am I really out of luck or is there some way to work around this? For many of my windows I can make them modal but I have a window that pops up when a solution is opened and making it modal makes it blocking and sometimes causes issues with VS as it's still trying to load the solution in the background asynchronously. On a side note, I'm thinking I'll open a PR with some extensions for opening a modal window to help with this as it's not as straight forward as simply calling |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I've seen similar behavior of a XAML control hosted in a tool window where focus backspace and other keys suddenly goes to the main editor instead of staying in the text field in my tool window. @AArnott @BertanAygun do you know how to send all keys to custom WPF controls? Is there a property to set on the |
Beta Was this translation helpful? Give feedback.
-
A tool window seems like the easier way to go instead of fighting against Visual Studio. Apparently you can set a tool window to always float by setting the [Microsoft.VisualStudio.Package.ProvideToolWindowAttribute(typeof(MyToolWindow), Style= Microsoft.VisualStudio.Shell.VsDockStyle.AlwaysFloat)] I tried it and I was still able to dock the window, but it did start out as a floating tool window. 🤷♂️ |
Beta Was this translation helpful? Give feedback.
-
@StevenRasmussen if you have a non-modal dialog, why would you want to intercept the keystrokes? |
Beta Was this translation helpful? Give feedback.
-
@StevenRasmussen Have you tried creating and showing the modal in a separate thread? The code below seems to work for me, but I'm not sure if I'm doing something that I shouldn't. I seem to recall @AArnott saying not to use dispatchers in Visual Studio, but I can't remember where he said that. It may have been in this Remote Office Hours video. private void ShowNonBlockingModal()
{
var thread = new Thread(() =>
{
try
{
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher)
);
var dialog = new ThemeWindowDialog()
{
DataContext = new ThemeWindowDialogViewModel()
};
dialog.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
dialog.Show();
Dispatcher.Run();
}
catch (Exception ex)
{
ex.Log();
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
}
}
);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
} |
Beta Was this translation helpful? Give feedback.
@StevenRasmussen Have you tried creating and showing the modal in a separate thread? The code below seems to work for me, but I'm not sure if I'm doing something that I shouldn't. I seem to recall @AArnott saying not to use dispatchers in Visual Studio, but I can't remember where he said that. It may have been in this Remote Office Hours video.