新的 WPF 窗口仅显示在原始窗口下方

发布于 2024-12-03 19:39:59 字数 1610 浏览 2 评论 0原文

在我的 WPF 应用程序中,主窗体上有一个 ListView,用于显示数据集中的绑定数据。当用户双击 ListView 中的一行时,会打开一个详细信息窗口。

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

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

在代码隐藏中,我有一个字典,用于跟踪打开的详细信息窗口(一次可以打开多个),以便如果详细信息窗户已经打开,它被带到前面。我像这样处理双击:

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),详细信息窗口将在主窗体顶部打开,但主窗体永远无法出现在详细信息窗口前面。

displayedCards[row].Activate() 按我的预期工作,将该详细信息窗口带到所有其他详细信息窗口的前面,但它遇到了与上面相同的问题 - 它没有出现在主窗口前面。

我想要完成的任务是让详细信息窗口与主窗口位于同一级别/层(/z 顺序?),以便两者都可以出现在彼此的顶部,并且让详细信息窗口显示在主窗口的顶部打开时的主窗体。

编辑:如果重要,详细信息窗口没有WindowStyle,并且AllowsTransparency设置为true。我也没有窗口标题,并且该窗口没有出现在任务栏中。当试图解决这个问题时,我尝试将 WindowStyle 设置为 SingleBorderWindow,并且出现了相同的问题,只是详细信息窗口的边框显示在主窗口的顶部当详细信息窗口被绘制时,它会被推到主窗体后面。在显示详细信息窗口后,我的双击处理程序实际上是否可以将主窗体拉到前面?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2024-12-10 19:39:59

您似乎没有告诉事件路由系统您已经处理了该事件。尝试将 e.Handled 设置为 true 以告诉 WPF 不允许进一步处理您的事件。请参阅 RoatedEventArgs.Handled 属性将路由事件标记为已处理和类处理,了解有关 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文