WPF window textbox - editing text affects differen

2019-03-05 04:16发布

问题:

I am writing a Visual Studio extension for our Dev team (a VSIX solution). When the user activates the extension, they get a form which is a very simple WPF window. The window is loaded using the following code:

var myWindow = new SomeWpfWindow(myArgs)
{
    Owner = Application.Current.MainWindow
};
myWindow.Show();

This opens and displays a form that the user fills in. The form has lots of TextBoxes that undergo 2-way binding to the DataContext. Nothing too out of the ordinary.

Well, apart from the TextBox behaviour....

When I type characters in the textbox ("blah blah blah") then I see these characters displayed in the text box, and they also get written to the DataContext property that they're bound to.

However. When I press the back-space or delete button, then the text in the TextBox remains unchanged, but instead the window behind (in this case, the active code file) is edited. Not ideal behaviour....

I fixed this by using .ShowDialog() rather than .Show(), but is is the correct fix, or am I just burying the problem?

Thanks

回答1:

VS doesn't really know about your window, and you'll run into problems with acceleratory keys and command routing.

The proper way to do this is to either implement a modal dialog (see my response to your other post regarding Microsoft.VisualStudio.PlatformUI.DialogWinodow).

Or you should implement a toolwindow.

Sincerely,



回答2:

The solution provided above solved the problem described above, but it wasn't the complete solution. What I found was that, having closed my WPF Dialog Window, I was then able to type into a C# window in the open Solution, but not able to Delete or back-space to remove text. Also when I tried to close Visual Studio, it gave the following warning:

Microsoft Visual Studio has detected that an operation is blocking user input. This can be caused by an active modal dialog or a task that needs to block user interaction. Would you like to shut down anyway?

Please refer to the following post for the solution to this: WPF modal window in Visual Studio Extension blocking input