开发者

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}}" Mou开发者_StackOverflow中文版seDoubleClick="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();
    }


Not sure... but does the issue get fixed if you remove your scrollviewer, and instead use:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜