A new WPF window is displayed only below the original window.

In my WPF application, I have a ListView in the main form, which displays the related data from the DataSet. When a user double-clicks a row in a ListView, it opens a details window.

In my XAML, I used a style to create a double-click handler in a list:

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

In the code, I have a dictionary that tracks open information windows (several can be opened at the same time), so if the details window is already open, it is brought to the foreground. I handle a double click like this:

 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 code like this above, detail windows open behind the main form. If I set the owner information ( window.Owner = this ), the detail windows will open on top of the main form, but the main form will never be able to get in front of the detailed windows.

displayedCards[row].Activate() works as I expected, bringing this detailed window to all other detailed windows, but it falls prey to the same problem as above - it does not appear in front of the main window.

What I want to accomplish is to have windows with parts on the same level / level (/ z order?) As the main window so that both of them can be displayed on top of each other and so that the detail windows appear on the top of the main form when they are open.

Edit: If this is important, there is no WindowStyle in the details window and AllowsTransparency set to true. I also do not have a window name, and the window does not appear on the taskbar. When trying to figure this out, I tried setting WindowStyle to SingleBorderWindow , and the same problem occurs, except that the frame of the part window is displayed on top of the main form when drawing the detailed window and then it pushed to the main form. Can my double-click handler pull the main form to the front after displaying the detail window?

+6
source share
1 answer

It looks like you are not telling the event routing system that you handled the event. Try setting e.Handled to true to tell WPF to prevent further processing of your events. See the RoutedEventArgs.Handled property and Marking routed events as processed and handling classes for more information on how the property handles modifies event routing.

+8
source

Source: https://habr.com/ru/post/897002/


All Articles