WindowsFormsHost控件似乎被设置为显示在顶部。有没有办法改变它的z顺序,使同一窗口上的其他WPF控件在WindowsFormsHost控件的顶部可见?
发布于 2010-08-24 19:51:49
不幸的是,没有,因为winformshost被组合成一个WPF窗口的方式,它必须出现在顶部。
请参阅here中的z顺序段落。
在WPF用户界面中,可以更改元素的z顺序以控制重叠行为。宿主的Windows窗体控件绘制在单独的HWND中,因此它始终绘制在WPF元素的顶部。
宿主的Windows窗体控件也绘制在任何Adorner元素的顶部。
发布于 2020-04-04 22:56:47
你可以玩点小把戏。当您声明一个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%s将超出ScrollViewer界限。但是有一种方法可以创建“中间”的HWND项目来在ScrollViewer上裁剪WinForms区域。只需放置另一个带有ElementHost的WindowsFormsHost,如下所示:
<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%s的剪辑区域为ElementHost,WinForms Button%s将在滚动时由它进行剪辑。您还可以为ContentContol创建ControlTemplate,并在需要的地方重用它。
<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>https://stackoverflow.com/questions/3556076
复制相似问题