question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ShowDialog forces ownership

See original GitHub issue

Describe the bug In WinForms and WPF, one can call ShowDialog() on a window instance without specifying the owner window. The new dialog is displayed on top of all the other opened windows of the application, which all become blocked until the dialog is closed.

To Reproduce Call ShowDialog() on a window instance, without specifying a parent window as argument.

Expected behavior As all the other UI frameworks work, a parent owner window should not be mandatory when displaying a dialog. This becomes important in situations like an abstract factory for views used in conjunction with a Dependency Injection system, when the DI system resolves a window from an interface, from a project that has no references to any view-related code, ie: business model in domain driven design. At the moment of using the abstract view factory to display a window, all info regarding window ownership is not available without adding a reference to Avalonia.Controls (where Window type is defined) or hacky workarounds.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kekekekscommented, Dec 20, 2021

@rusoaica if your app is guaranteed to have only one toplevel window with everything else being a dialog, then it’s fine to keep it as a singleton on the UI layer.

Otherwise I’d suggest to use a technique that tracks active views for a particular view model. For example you could have a base class for your views that would keep a shared Dictionary<ViewModelBase, List<ViewBase>> and update its contents in OnAttachedToVisualTree, OnDetachedFromVisualTree and on DataContext changes. That way your UI layer will know which visible controls are currently associated with a particular view model, so you could get the appropriate toplevel Window to use as a parent.

1reaction
rusoaicacommented, Dec 20, 2021

@kekekeks the workaround I ended up using is to store the hash code of each window inside its view model, and pass that as argument… when displaying a dialog, I am iterating all opened windows of the application, and set as parent the one that matches the provided hash code. As I said, looks hackish, but works:

For the DI container registration:

builder.RegisterType<StartupV>().OnActivating(e =>
{
    e.Instance.DataContext = e.Context.Resolve<IStartupVM>();
    (e.Instance.DataContext as IBaseModel).ParentHashCode = e.Instance.GetHashCode(); // store window hash code inside viewmodel
}).As<IStartupView>().InstancePerDependency();

After that:

public async Task<bool?> ShowDialogAsync(int parentHashCode)
{
    if (parentHashCode != 0)
    {
        IReadOnlyList<Window>? windows = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime).Windows;
        Window? parent = windows.Where(w => w.GetHashCode() == parentHashCode).First();
        return await ShowDialog<bool?>(parent);
    }
    else
        return await ShowDialog<bool?>(StartupV.Instance); // use first application window as singleton, if no hashcode is provided
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Form.ShowDialog() or Form.ShowDialog(this)?
Although ShowDialog establishes an implicit owner-owned relationship, there is no built-in way for the owned form to call back to or query the ......
Read more >
Form.ShowDialog Method (System.Windows.Forms)
Shows the form as a modal dialog box with the specified owner. ... The example uses the version of ShowDialog that specifies an...
Read more >
Dialog
For example, if Activity#showDialog(int) is used to show this Dialog, that Activity will be the owner (by default). Depending on how this dialog...
Read more >
[Solved] Bring a window to front
The only situation in which I have seen a dialog not come to the front was when the creating window was TopMost and...
Read more >
wxDialog Class Reference
Modal and Modeless ; Call this function to force one or both scrollbars to be always shown, even if the window is big...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found