【问题标题】:WPF Main Window is always above other windowsWPF 主窗口始终位于其他窗口之上
【发布时间】:2016-05-26 20:49:00
【问题描述】:

有很多代码。但问题是: 我有一个带有控件的列表框作为数据模板:

<ListBox x:Name="UpcomingConcertsList" ItemsSource="{Binding UpcomingConcerts}" HorizontalAlignment="Left" Height="350" Margin="10,208,0,0" VerticalAlignment="Top" Width="370">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Control MouseDoubleClick="UpcomingConcert_DoubleClick">
                    <Control.Template>
                        <ControlTemplate>
                            <Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4" Width="320">
                                <Grid Margin="3">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="100"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Image Grid.RowSpan="2" Width="100" Height="75" Margin="6" Source="{Binding ImageURL}"/>
                                    <StackPanel Grid.Column="1" Margin="2,6">
                                        <TextBlock FontWeight="Bold" Text="{Binding Name}"/>
                                        <TextBlock Text="{Binding Date, StringFormat={}{0:g}}"/>
                                        <TextBlock Text="{Binding Bands, Converter={StaticResource BandsConverter}}"/>
                                    </StackPanel>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Control.Template>
                </Control>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

以及后面的点击事件代码:

private void Concert_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        Control control = sender as Control;
        Concert concert = control.DataContext as Concert;

        ConcertView wndw = new ConcertView(concert.ConcertID);

        wndw.Show();
    }

ConcertView 窗口已打开,但位于我的 MainWindow 下方。 wndw.Activate()、wndw.Focus() 没有帮助。 我试图这样做。IsEnabled = false 和 wndw.Show() 在此之后。然后我的 ConcertView 在 MainWindow 上方。但是当 this.IsEnabled 变为真时,ConcertView 突然崩溃了。

有什么想法吗?

【问题讨论】:

  • 你试过wndw.TopMost = true; ??
  • @vishakh369 是的,我试过了。当然,我的 wndw 超越了所有的窗户。但 TopMost 使我的窗口覆盖所有应用程序。但我只需要我的 ConcertVIew 出现在 MainWindow 上方。我还在 Thread.Sleep(x) 之后尝试了 wndw.TopMost = true 和 wndw.TopMost = false (睡眠只是为了检查它是否工作正常) - 所以我的 wndw 得到了顶部,之后进入了 MainWindow
  • 我将我的回复作为答案发布,因为它太长并且包含代码。无论如何,我认为它应该工作。

标签: c# wpf events window


【解决方案1】:

但 TopMost 让我的窗口覆盖所有应用程序 你这是什么意思?您当前正在使用一个应用程序,当您单击一个按钮时,会加载一个新窗口。所以你想让新窗口在最上面吗?

您可以设置窗口的 Owner 属性来设置它的所有者。即主窗口。如果您设置 this,则当前窗口将是所有者窗口。

Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.Show();

现在尝试设置wndw.TopMost = true; 并检查其是否正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2018-07-17
    相关资源
    最近更新 更多