0
votes

When my application shows the main window plus on top of that a smaller modal window (which was opened with ShowDialog) I have a problem with ALT/TAB. When I go to another application with ALT/TAB and then return to my application, only the modal window shows, the main window has disappeared. It is only an optical thing. There is no functional problem. The main window has ShowInTaskbar="False" because I do no wish the user to be able to bring to front the inactive main window with ALT/TAB. Does anybody know a solution to this problem?

1
Dialog windows will always stay on top so you don't need ShowInTaskbar="False".user5226582

1 Answers

1
votes

I have made this very simple sample:

In your MainWindow.xaml

<Window x:Class="SampleDialog.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Show Dialog" Click="ButtonBase_OnClick"></Button>
    </Grid>
</Window>

In codebehind

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            new Dialog(this).ShowDialog();
        }
    }

In Dialog.xaml

<Window x:Class="SampleDialog.Dialog"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Dialog" Height="300" Width="300" 
        Activated="Dialog_OnActivated" Deactivated="Dialog_OnDeactivated">
    <Grid>

    </Grid>
</Window>

And in Dialog codebehind

public partial class Dialog : Window
{
    public Dialog()
    {
        InitializeComponent();
    }

    public Dialog(MainWindow main)
        : this()
    {
        _main = main;
    }

    private void Dialog_OnActivated(object sender, EventArgs e)
    {
        Topmost = _main.Topmost = true;
    }

    private void Dialog_OnDeactivated(object sender, EventArgs e)
    {
        Topmost = _main.Topmost = false;
    }

    private readonly MainWindow _main;

The intention is to handle this functionality in Activated and Deactivated events on your modal window.

Hope it helps.

(If you are using MVVM then you must refactor to handle this on a WindowService class or by using EventToCommand approach)

EDIT>>> This works for your case when ShowInTaskbar = "False". =)