I have a basic WPF application based on MVVM, in that i have to implement save prompt if the user has any unsaved changes on the screen and he tries to close it without saving.
For the same i have handled the closing event of the window and i am presenting a prompt.
My problem is i see the DataContext is stale, it's not having the latest changes in UI.
One more observation is that i see the correct DataContext on closed event of the window.
Any clues..?
Binding would not have stopped when your Window is Closing and will continue to work. Have you checked output for binding errors?
Show me the code :)
Related
I have a WPF application with a MainWindow and a couple of Dialogs that appear over MainWindow.
I want my main window to be activated and appear on top of all dialogs when its clicked. I have tried method Activate() but its not working ,also tried Topmost which is also not working ?
Please suggest a solution.
I suppose you are using ShowDialog() to open the other dialogs. Try using Show() when opening new dialogs as that won't force you to close them before being able to access the MainWindow. Of course without a bit more information on your code it's impossible to know exactly what causes the problem.
I have a winform application with data grid rows with some icons.
When the user clicks on one of the icons, a WPF window opens.
I have created this WPF window using Prism i.e. it has shell and regions mapped to view.
The issues I am facing is:
When I tried to close the WPF window, I get the exception "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."
I understand that we can resolve the issue by hiding the window instead of closing it.
However, this makes my ViewModel and Services representing the older WPF window.
I have kept the static counter in the ViewModels and observed that every time, I open the WPF window, static count increases which means my old view models are not getting destroyed.
I would like how to handle this scenario correctly so that when I close the window everything related to the window should be disposed off.
I tried to do container.dispose in ShellViewModel, however, still it did not work.
There are two aspects here. Firstly, you can use either RegionMemberLifetimeAttribute on your view model or implement IRegionMemberLifetime to make Prism create a new instance each time.
Secondly, you have to create your own RegionBehavior (or take it from this Github Issue) to make Prism dispose view models.
I am new to WPF and have two windows in a solution. Both are opened using Window.Open().
However there is a difference in behaviour that I don't understand. When one of the windows is opened, if you click the view that launched it the new window is minimised behind the main view, so you can interact with it.
The other window, when opened and you click the main view stays where it is and does not get hidden.
How can I get this window to show the same behaviour as the first?
I figured this out, by removing the parent child relationship in the window I was able to get the functionality I desired.
My WPF application has windows that are running on separate dispatchers. I need to be able to tell those windows to activate when the main window has focus. What event should I listen too to know when the application has focus?
Application.Activated event is a good place. When you get this event, your application has been activated by the user (either mouse click or keyboard focus switched to it).
It's not clear from the question if you want to know about the application getting focus (in which case, Franci is right) or when the main window gets focus. For the main window (or any particular window), there is a GotFocus event inherited from UIElement http://msdn.microsoft.com/en-us/library/system.windows.uielement.gotfocus.aspx
I've been working on injecting input into a WPF application. What makes this project hard is that I need to be able to inject the input into the application even though it's running in the background (i.e. another application has the input focus). Using the SendInput() function is therefore out of the question.
So far, I've got keyboard input working but am having trouble injecting mouse input.
I used Spy++ to observe the window messages that get sent to the WPF window when I physically click the mouse button. I then simply craft these same mouse messages (such as WM_LBUTTONDOWN and WM_LBUTTONUP) manually and send them explicitly to the WPF window to emulate mouse input.
Unfortunately, this doesn't work as expected (not even when I, for testing purposes, have set the WPF window as the foreground window).
I've added a button to my test WPF window which when clicked displays a message box. Injecting the appropriate mouse messages when I've manually positioned the cursor over the button doesn't cause the button to be clicked, however (i.e. the clicked event isn't fired by the WPF framework).
If I add a handler for mouse clicks on the actual dialog (the client area), that handler does get called if I position the cursor over the dialog itself and inject the same window messages as before:
this.MouseLeftButtonDown += WndMouseDown;
public void WndMouseDown(object sender, EventArgs e)
{
...
}
Strangely enough, if I change the push mode of the button to Press (i.e. it's considered clicked on mouse down rather than the default mouse up), the button clicked event is now fired when I inject the same messages as before. (It's worth mentioning that the handler from the example above correctly fires for both mouse downs and ups, so it'd seem the WPF framework does process both messages successfully.)
It seems like there are some other criteria that need to be fulfilled in order for a mouse clicked event to be fired by the WPF framework. Does anybody know how mouse input is handled internally in WPF, or why it's not interpreting my mouse up and down messages as a click on the button?
(It's worth mentioning that this approach [sending window messages] works fine on ordinary Win32 windows, such as the Start->Run dialog. The difference here is that WPF only has one physical Win32 window and the rest is WPF specific, which means all window messages go to that top-level window rather than the actual button.)
I've been searching high and low for an answer to this and would appreciate any thoughts or ideas.
I'd highly suggest going the UIAutomation route. You create an AutomationElement by window handle. Crawl to the button and invoke it. I'd just like to know how you managed to get the keyboard input working. I am currently trying to resolve the converse issue. How to get a WPF window (I've managed to get a hWnd to it via Win32 calls), to respond to virtual keyboard messages. I've logged ++spy sessions on the window in question and replicated it's input without success.
Use UI Automation to do this - trying to manually simulate input via window messages is a textbook mistake, like trying to start a land war against Russia.
Your strategy is basically sound but in order to send a message to a window owned by another process you must first register the message.
Here is an article explaining the whole business. The sample code is unfortunately in VB but I'm sure that won't stop you.