新的WPF窗口仅在原始窗口下方显示 [英] New WPF window only shows underneath originating window

查看:275
本文介绍了新的WPF窗口仅在原始窗口下方显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,主窗体上有一个ListView,用于显示来自DataSet的绑定数据.当用户双击ListView中的一行时,它将打开一个详细信息窗口.

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.

在我的XAML中,我使用了一种样式在列表视图上创建双击处理程序:

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}>

在后面的代码中,我有一个Dictionary,用于跟踪打开的Details窗口(一次可以打开多个),这样,如果一个detail窗口已经打开,它就会放在最前面.我这样处理双击:

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();
    }
}

我的问题是,使用上面的代码,详细信息窗口在主窗体后面打开.如果设置所有者信息(window.Owner = this),则会在主窗体顶部打开详细信息窗口,但是主窗体永远无法出现在详细信息窗口的前面.

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.

displayedCards[row].Activate()的工作与我预期的一样,使该详细信息窗口位于所有其他详细信息窗口的前面,但是却成为上述问题的受害者-它不在主窗口的前面.

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.

我要完成的是将详细信息窗口与主窗口置于相同的层/层(/z顺序?)上,以便两者可以彼此叠加显示,并在其上显示详细信息窗口.当它们打开时位于主窗体的顶部.

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.

如果重要,则详细信息窗口中没有WindowStyleAllowsTransparency设置为true.我也没有窗口标题,并且该窗口没有出现在任务栏中.当试图弄清楚这一点时,我尝试将WindowStyle设置为SingleBorderWindow,并且发生了相同的问题,不同之处在于,在绘制详细信息窗口时,详细信息窗口的边框显示在主窗体的顶部,并且然后将其推入主表单的后面.在显示详细信息窗口之后,我的双击处理程序能否将主窗体拉到最前面?

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?

推荐答案

似乎您没有在告诉事件路由系统您已经处理了事件.尝试将e.Handled设置为true可以告诉WPF不允许对事件进行任何进一步处理.请参见 RoutedEventArgs.Handled属性

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.

这篇关于新的WPF窗口仅在原始窗口下方显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆