简体   繁体   中英

Why does WPF ScrollViewer causes window to open behind main window?

I have an items control on my window and when its double clicked I want to open a second window. My problem is that if the items control is wrapped in a scroll viewer the new window comes up behind the main window instead of in front of it. If comment out the scroll viewer in this code the window opens in front as intended.

Whats going on here?


Window XAML:

<Window x:Class="EktronDataUI.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TestWindow" Height="300" Width="300">
    <Grid>
        <ScrollViewer>
            <ItemsControl ItemsSource="{Binding Source={StaticResource odpMockSmartForms}}" MouseDoubleClick="ItemsControl_MouseDoubleClick" >
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="Double Click Me" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>

Code Behind:

    private void ItemsControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TestWindow window = new TestWindow();
        window.Show();
    }

Have you tried telling the MouseButtonEventArgs that you handled it? The ScrollViewer is most likely trying to focus or do something else when you double click inside it, causing the window to become active again after the other window is opened.

private void ItemsControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        e.handled = true;
        TestWindow window = new TestWindow();
        window.Show();
    }

不确定...但是如果删除scrollviewer,该问题是否得到解决,而是使用:

<ItemsControl ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>

I pulled in your code and got it to pull to the front if I set the TopMost equal to true.

private void ItemsControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        TestWindow window = new TestWindow();

        window.Show();
        window.Topmost = true;

    }

Is this what you're looking for?

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