简体   繁体   中英

WPF Main Window is always above other windows

There is much of code. But problem is there: I've got a Listbox with Control as DataTemplate:

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

And click event code behind:

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

And ConcertView window is opened but just under my MainWindow. wndw.Activate(), wndw.Focus() don't help. I tried to do this.IsEnabled = false and wndw.Show() after this. Then my ConcertView was above MainWindow. But as this.IsEnabled comes to true, ConcertView suddenly goes under.

Is ther eany ideas?

But TopMost makes my window being over all the applications What do you mean by this? You're currently using an application and when you click a button, a new window is loaded. So you want the new window to be at topmost itself right?

You can set the Owner property of a window to set it's owner. ie the MainWindow . If you set this then the current window will be owner window.

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

Now try setting the wndw.TopMost = true; and check if its working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM