WPF 列表框中的 WindowsFormHost 项 - Z 顺序
我已经到了网络的尽头并回来了,但我似乎找不到任何解决我的 Z 排序 WindowsFormsHost 问题的解决方案。我的问题特定于渲染 ListBox,其中项目是 WindowsFormsHost 包装的 WinForm 控件。渲染时,所有项目都会显示,即使是列表框范围之外的项目 - 这使得整个屏幕看起来很糟糕。
我附上了一个快速代码示例。我有什么选择吗?我需要重新考虑我的布局吗?
<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);
}
}
请帮忙:)
I have been to the end of the web and back and I can't seem to find any solution for my Z-Ordering WindowsFormsHost Issue. My issue is specific to rendering a ListBox where the items are WindowsFormsHost wrapped WinForm controls. When rendered all the items are displayed even those outside the bounds of the ListBox -- which makes the entire screen look horrible.
I am attaching a quick code sample. Do I have any options? Do I need to rethink my layout?
<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>
and the codebehind:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = System.Linq.Enumerable.Range(0, 30);
}
}
Please help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这称为 AirSpace 问题。 Winforms 将在 WPF 之上进行渲染,因为两者使用不同的渲染技术。有一些解决方案,但并不简单。
查看此博客文章 或此处,您可能可以开始工作吧。
如果这不起作用,我建议不要将 WinForms 放置在低于顶层的任何位置(即在窗口上),并确保您不会对布局进行任何花哨的操作。
This is known as the AirSpace issue. Winforms will render above WPF since both use different rendering technologies. There are a few solutions around but are not straight forward.
Have a look at this blog entry or here which you might be able to get working.
If this doesn't work I suggest do not place WinForms anywhere lower than the top level (i.e. on the window) and make sure you do not do anything fancy with layouts.