WindowsFormsHost ZOrder

问题描述 投票:6回答:2

看来WindowsFormsHost控件设置为显示在顶部。是否可以更改其z顺序以使同一窗口上的其他WPF控件在WindowsFormsHost控件顶部可见?

wpf z-order
2个回答
6
投票

不幸的是,由于winformshost组合到WPF窗口中的方式,它必须出现在顶部。

请参见here中的z顺序段落。

在WPF用户界面中,您可以将元素的z顺序更改为控制重叠行为。绘制了一个托管的Windows窗体控件在单独的HWND中,因此始终将其绘制在WPF元素之上。

托管的Windows窗体控件也绘制在任何Adorner的顶部元素。


0
投票

您可以做一些技巧。声明WindowsFormsHost时,其父级是第一个HWND组件。通常是根窗口。因此,控件的剪辑区域是整个窗口。我将使用WPF ScrollViewer展示一个示例。

<Window>
    <Grid>
        <ScrollViewer Margin="20,50">
            <ItemsControl ItemsSource="{StaticResource StringArray}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <WindowsFormsHost>
                            <wf:Button />
                        </WindowsFormsHost>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>

在这种情况下,Button将超出ScrollViewer范围。但是,有一种方法可以创建“中间” HWND项,以将WinForms区域裁剪到ScrollViewer上。只需将另一个WindowsFormsHostElementHost放置在一起,如下所示:

<Grid>
    <WindowsFormsHost Margin="20,50">
        <ElementHost x:Name="This is a clip container">
            <ScrollViewer>
                <ItemsControl ItemsSource="{StaticResource StringArray}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <WindowsFormsHost>
                                <wf:Button />
                            </WindowsFormsHost>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </ElementHost>
    </WindowsFormsHost>
</Grid>

Button的当前剪辑区域为ElementHost,并且在滚动时将由WinForms Button剪辑。您也可以为ControlTemplate创建ContentContol并在需要的地方重复使用。

<ControlTemplate x:Key="ClipContainer" TargetType="{x:Type ContentControl}">
    <WindowsFormsHost>
        <ElementHost>
            <ContentPresenter />
        </ElementHost>
    </WindowsFormsHost>
</ControlTemplate>
<Grid>
    <ContentControl Template="{StaticResource ClipContainer}" Margin="20,50">
        <ScrollViewer>
            <ItemsControl ItemsSource="{StaticResource StringArray}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <WindowsFormsHost>
                            <wf:Button />
                        </WindowsFormsHost>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </ContentControl>
</Grid>
© www.soinside.com 2019 - 2024. All rights reserved.