WPF ListBox中的WindowsFormHost项目 - Z-Order

3

我已经搜索了整个网络,但似乎找不到解决我的Z-Ordering WindowsFormsHost问题的方法。我的问题特定于呈现一个ListBox,其中项目是WindowsFormsHost包装的WinForm控件。当渲染时,所有项目都会显示,即使超出了ListBox的边界 - 这使整个屏幕看起来很糟糕。

我附上了一个简短的代码示例。我有什么选择吗?我需要重新考虑我的布局吗?

    <Window x:Class="WFH_ZOrderIssue.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  Title="WFH ListBox ZOrder" Height="300" Width="600">
 <Grid>
  <ListBox Background="LightBlue" HorizontalAlignment="Left" ItemsSource="{Binding}" MaxWidth="400" BorderThickness="0" 
     ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False" 
     ScrollViewer.HorizontalScrollBarVisibility="Auto"
     SelectionMode="Extended" x:Name="lstDisplays" 
     >
   <ListBox.ItemTemplate>
    <DataTemplate>
     <ScrollViewer x:Name="viewer" VerticalScrollBarVisibility="Hidden" >
      <wfi:WindowsFormsHost Margin="20,0" x:Name="host">
       <wf:Button Text="WindowFormsHost - The Ugly" />
      </wfi:WindowsFormsHost>
     </ScrollViewer>
    </DataTemplate>
   </ListBox.ItemTemplate>
   <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
     <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
  </ListBox>
 </Grid>
</Window>

还有代码后端:

    /// <summary>
 /// Interaction logic for Window1.xaml
 /// </summary>
 public partial class Window1 : Window
 {
  public Window1()
  {
   InitializeComponent();
   this.DataContext = System.Linq.Enumerable.Range(0, 30);
  }
 }

请帮忙 :)
需要翻译的内容已经翻译完毕。
1个回答

1

这被称为AirSpace问题。由于两者使用不同的渲染技术,因此Winforms将在WPF之上呈现。有一些解决方案,但并不直接。

看看这篇博客文章这里,你可能能够让它们正常工作。

如果这行不通,我建议不要将WinForms放置在低于顶层(即窗口)的任何位置,并确保不要对布局进行任何花哨的操作。


1
两个链接已不再可用。 - RJFalconer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接