How to Activate MainWindow in wpf application? - wpf

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.

Related

Removing ViewModels in Prism when Closing Window

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.

Creating a multiple WPF application using Catel

I have started a new project using WPF and Catel and I want to be able to have multiple windows without ownership. I have been able to create an application with a standard window and which creates dialogs. I would like to find an example or tutorial on how to create a window and then close the current window.
Example:
Window1 -> Select Open Window2 Button -> Open Window2 -> Close Window1
Thanks
When you call Show on the UIVisualizer, it should show the window non-modal. After calling the UIVisualizer service, just call CloseViewModel on yourself and the current window should close.
It appears that the above answer is correct as long as you are not closing the startup window.
It appears that when you close the ViewModel that is targeted as your startup View\ViewModel when you close it, it closes the whole application.
My question now.. How can I prevent this from happening.

New window in Silverlight out-of-browser application

In a Silverlight application that doesn't run in a browser, is it possible to create a new top-level window? Or at least a child window?
I found some solution using the ChildWindow class, but even though my project is configured for Silverlight 4, that class can not be found.
I have a UserControl (XAML file) that I want to show as a new window. Using a tab control is not really an option unfortunately as the user has to be able to arrange windows to see more than one at once.
Any suggestions?
ChildWindow is part of the SDK, you need to add the System.Windows.Controls.dll to access the ChildWindow type.
I'm not sure you can get the ChildWindow to do what you are expecting. A ChildWindow is designed to present a window in a modal manner. However this modal behaviour is really a function of the ChildWindow template. It is possible to re-template to remove the modal behaviour. However I've never tried to manipulate multiple child windows. You could give it a go, the big question would be what happens if you close Child windows in a different order in which they were created?
In order to use a ChildWindow to present your UserControl it would probably be best for you to derive from ChildWindow, instead of UserControl. You may even find it would ultimately be better for you create base class between your specific Xaml and ChildWindow where you would put code that is common to all your windows.
I might be worth you noting the Silverlight 5 will support multiple windows.

View never becomes visible when implementing Caliburn + MEF on existing WPF App

I have an existing WPF project and I want to move it over to caliburn with MEF.
My shell is a viewmodel called MainViewModel this opens up a dialog using the DefaultWindowManager this is a login window this all works fine but after logging in the program stalls for a little bit then exits. and the MainView never becomes visible. With debugging I checked and verified that the MainView does get created it just never becomes active or visible.
I tried inheriting my MainViewModel from Screen, IShell and NotifyPropertyBase, IShell and IShell. Does anyone have any ideas or clues for me to try thanks.
I am not using caliburn in the MainView and ViewModel besides for the view to view model linking as I just took the old view/view models could this have to do with anything? Is there a way to turn of the ViewModelBinder for certain viewmodels/views?
I'll mention this in case your issue is the same as I had, as it slowed me down somewhat recently... If your Shell has some Imports that are not being satisfied then the Shell won't appear, and the effect is rather confusing.
Ensure that you have [Import(AllowDefault=True)] as your Attribute or use [ImportMany]
However, since you do say that in debugging there is an instance, the issue might be in the way you are using Caliburn(Micro?). In this case, I suggest you copy a Caliburn example and then add your functionality - least then you'll know it once worked.
Rgds
John
Actually Rob Eisenberg of caliburn was very helpful and he helped me out with this issue.
The problem was that when I switched to caliburn the LoginView was the first window to be opened and it was closed before the MainView window was opened.
windows treats the first window opened as the mainwindow. and when the mainwindow is closed windows checks if other windows are open if not it closes the application.
He provided a possible solution of making the loginviewmodel the shell and closing it after opening the mainviewmodel.

Mixing MFC and WPF: Modal Dialogs

I'm adding C# WPF dialogs to an existing C++ MFC app, using a C++/CLI interface layer. I've got things working, except I'm having a problem with modality. For example:
MFC app shows a WPF dialog using ShowDialog. Works as expected.
That WPF dialog shows a MFC dialog using DoModal. The WPF dialog is hidden behind the base C++ app, and is not disabled unless I manually change IsEnabled. Not ideal, but it works.
Now, that MFC dialog is closed. Now for some reason the base MFC app is enabled, when it should still be disabled due to the WPF dialog not having been closed. That's bad, as it now allows the user to do crazy things while the WPF dialog is still open.
I have a feeling that it would work better if I could set parent dialogs correctly. But so far I havent been able to set an MFC dialog's parent as a WPF dialog, or vice versa. And, I don't even know if that'd fix it.
Any ideas?
When opening a CDialog, the trick is to use a WindowsInteropHelper to get the parent WPF dialog's HWND. Then, you can use CWnd::Attach to wrap that HWND in a CWnd class to pass to the CDialog's constructor.
The problem I had was that I already had the CDialog constructed., but not yet displayed. The various versions of SetParent can only be used if your target child window already has a valid handle. I had to write a new function in my CDialog class to set m_wndParent, which is what it uses as the parent when it finally creates the dialog. Then everything works great!
Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.
When showing the WPF dialog, are you using the HwndSource class to wrap the WPF window? If so, you may be able to ::SetParent the WPF window as well as use the HwndSource.Handle property to set the sub-child's parent.

Resources