Add support for hosted in winforms
See original GitHub issueIs your feature request related to a problem? Please describe.
Given a winforms form, that has an ElementHost
with a Child
set to a wpf UIElement
.
So there is no real wpf Window, just the ElementHost holding the wpf control.
When attempting to show a dialog somewhere inside the wpf control, the DialogService cannot find the owner window of the ownerViewModel and throws an exception
https://github.com/FantasticFiasco/mvvm-dialogs/blob/59a356e3e35b280a5147f0ff9281ed1f11289c30/src/net/DialogService.cs#L322
while trying to set the owner:
https://github.com/FantasticFiasco/mvvm-dialogs/blob/59a356e3e35b280a5147f0ff9281ed1f11289c30/src/net/DialogService.cs#L279
Describe the solution you’d like When a window of the ownerViewModel cannot be found, search for a winform e.g. with this extension method:
public static Form FindForm(this Visual element)
{
HwndSource wpfHandle = PresentationSource.FromVisual(element) as HwndSource;
if (wpfHandle == null)
{
return null;
}
ElementHost elementHost = System.Windows.Forms.Control.FromChildHandle(wpfHandle.Handle) as ElementHost;
return elementHost.FindForm();
}
Having that form, one can now set the owner of the WindowWrapper.window:
var helper = new WindowInteropHelper(window);
helper.Owner = ownerForm.Handle;
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
That’s a justifiable decision. The problem was solved, by dropping this library, but adapting the concept und creating the view manually and propagating the dialogResult from the viewmodel manually. The ViewModel and the view could still be used at is, there were just changes about 20 lines to be made. Nevertheless, thanks for your attention
Please delete: sebastian.knauer@ibgndt.demailto:sebastian.knauer@ibgndt.de
Correct address: s.knauer@ibgndt.demailto:s.knauer@ibgndt.de
Tnx.
Von: Mattias Kindborg @FantasticFiasco [mailto:notifications@github.com] Gesendet: Donnerstag, 16. Januar 2020 09:14 An: FantasticFiasco/mvvm-dialogs mvvm-dialogs@noreply.github.com Cc: sknauer-ndt sebastian.knauer@ibgndt.de; Author author@noreply.github.com Betreff: Re: [FantasticFiasco/mvvm-dialogs] Add support for hosted in winforms (#96)
Well that sounds like the perfect solution. A dependency, even to MVVM Dialogs 😀, comes with a cost. If you are able to solve your problem without this dependency, we are both better off.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/FantasticFiasco/mvvm-dialogs/issues/96?email_source=notifications&email_token=AOIYO6F3KXGLN4FBPQI35W3Q6AJOFA5CNFSM4KGRFXUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJDFNPA#issuecomment-575035068, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AOIYO6FOPF3W3VD5XWLWEJTQ6AJOFANCNFSM4KGRFXUA.