Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New WPF window only shows underneath originating window

In my WPF application, I have a ListView on the main form that displays bound data from a DataSet. When a user double-clicks a row in the ListView, it opens a detail window.

In my XAML, I used a style to create a double click handler on the listview:

<Style x:Key="ListViewDoubleClick" TargetType="{x:Type ListViewItem}">
    <EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
</Style>
...
<ListView Name="searchResults" ItemContainerStyle="{StaticResource ListViewDoubleClick}>

In the code-behind, I have a Dictionary that keeps track of the open Details windows (multiple can be open at a time) so that if a detail window is already open, it's brought to the front. I handle the double-click like so:

private void HandleDoubleClick(object sender, MouseEventArgs e)
{
    DataRowView clickedRow = ((ListViewItem)sender).Content as DataRowView;
    int row = (int)clickedRow.Row["ID"];
    if (!displayedCards.ContainsKey(row))
    {
        DetailWindow window = new DetailWindow(RetrieveData(row));
        //window.Owner = this;
        displayedCards.Add(row, window);
        window.Show();
    }
    else
    {
        displayedCards[row].Activate();
    }
}

My problem is that with the code like it is above, the detail windows are opened behind the main form. If I set the owner information (window.Owner = this), the detail windows are opened on top of the main form, but the main form is never able to come in front of the detail windows.

The displayedCards[row].Activate() works as I expected, bringing that detail window to the front of all other detail windows, but it falls victim to the same problem as above - it doesn't come in front of the main window.

What I want to accomplish is to have the detail windows on the same level/layer (/z order?) as the main window so that both can appear on top of one another, and to have the detail windows show up on top of the main form when they are opened.

Edit: If it's important, the detail window has no WindowStyle and AllowsTransparency is set to true. I also don't have a window title and the window doesn't appear in the taskbar. When trying to figure this out, I tried setting the WindowStyle to SingleBorderWindow, and the same problem occurs, except that the border of the detail window is shown on top of the main form as the detail window is being drawn, and then it's pushed behind the main form. Could my double-click handler essentially be pulling the main form to the front after the detail window is shown?

like image 585
Jared Harley Avatar asked Sep 09 '11 16:09

Jared Harley


1 Answers

It looks like you aren't telling the event routing system that you have handled the event. Try setting e.Handled to true to tell WPF to not allow any further processing of your events. See RoutedEventArgs.Handled Property and Marking Routed Events as Handled, and Class Handling for more information on how the Handled property changes event routing.

like image 158
Joshua Avatar answered Oct 19 '22 09:10

Joshua