kollodziej
kollodziej

Reputation: 35

WPF Child Window Disappears When Switching Between Applications (Topmost property set to true overlaps other applications)

'm working on a WPF desktop application and I'm experiencing an issue with how my child windows behave when switching between applications. Specifically, when I switch to another application using the taskbar and then return to my WPF application, the child window that should be displayed on top of the parent window is not visible. Instead, I see the main application window, and the child window seems to be hidden behind it.

I've tried using the Topmost property in WPF XAML to keep the child window on top of other applications, but this results in the child window appearing on top of all other applications, which is not the behavior I want.

I need the child window to stay on top of its parent window within my WPF application. When switching back to my application from another one, I want this specific child window to be visible and on top, blocking the view of the parent window and other controls within the application.

How can I achieve this behavior?

I have tried: How to make a WPF window be on top of all other windows of my app (not system wide)?

Here is my code in XAML:

<Window x:Class="FactoryConfigWpfViews.Views.ClientMultiServer.AddServerDialogWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    AutomationProperties.AutomationId="AddServerDialogWindow"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    ShowInTaskbar="False"
    Topmost="True"
    WindowStartupLocation="CenterScreen"
    ResizeMode="NoResize"
    mc:Ignorable="d"
    Title="{Binding WindowTitle}" Height="160" Width="450">
<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Width" Value="100" />
        <Setter Property="Height" Value="22" />
    </Style>
</Window.Resources>
<DockPanel FocusManager.FocusedElement="{Binding ElementName=ServerHostName}">
    <Grid DockPanel.Dock="Top" Margin="10,10,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="5,5,5,5" />
            </Style>
        </Grid.Resources>
        <Label Grid.Row="1" Grid.Column="0" Content="Enter the Server Identifier:" AutomationProperties.AutomationId="AddServerAssignedToClientServerHostnameLabel" Grid.ColumnSpan="2" Margin="0,10,275,-15" />

        <TextBox x:Name="ServerHostName" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ServerHostname, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AutomationProperties.AutomationId="AddServerAssignedToClientTextBox" Margin="153,12,10,-12"/>
    </Grid>

    <StackPanel DockPanel.Dock="Bottom" VerticalAlignment="Bottom" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,10,10">
        <Button Content="OK" HorizontalAlignment="Right" Margin="0,0,10,0" AutomationProperties.AutomationId="AddServerAssignedToClientOkButton" Command="{Binding AddServerAssignedToClient}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
                IsDefault="True"/>
        <Button Content="Cancel" AutomationProperties.AutomationId="AddServerAssignedToClientCancelButton" Command="{Binding CancelAddServer}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" 
                IsCancel="True"/>
    </StackPanel>
</DockPanel>

Upvotes: 0

Views: 39

Answers (0)

Related Questions