我想要将windows窗体控件包装在wpf UserControl中。
<UserControl x:Class="MVVMLibrary.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ws="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
Height="Auto" Width="Auto">
<Grid>
<WindowsFormsHost Name="Host">
<ws:ReportViewer/>
</WindowsFormsHost>
</Grid>
</UserControl>请注意,Height和Width均为auto。
当我把它放在堆叠面板或网格控件中时,它的高度设置为0,基本上就消失了。然后用户需要调整窗口的大小(缩小是因为用户控件说我不需要空间,谢谢)。当用户调整大小时,它会伸展到用户指定的任何位置。
所以我的问题是我做错了什么?如何让我的用户控件占用所有可用的空间,而不是不请求任何空间?
发布于 2010-09-21 20:26:54
我找到了一个更好的答案。
将dockPanel与LastChildFill=true一起使用,然后将具有水平和垂直对齐的WindowsFormsHost放入Strech,当然WindowsForms控件也必须填充。
<DockPanel LastChildFill="true">
<WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Panel Dock=Fill />
</WindowsFormsHost>
</DockPanel>享受‘,
发布于 2014-05-13 02:10:32
一些更正:默认情况下,DockPanel已启用LastChildFill,水平路线和垂直路线也会自动设置为拉伸。
因此,简洁的答案是:使用DockPanel
<DockPanel>
<WindowsFormsHost>
</WindowsFormsHost>
</DockPanel>发布于 2012-01-18 18:55:38
这里也有同样的问题。我所做的是将WindowsForm控件(必须嵌入)维度绑定到父主机(即WindowsFormHost)的维度(宽度和高度)。请参阅下面的代码。这是在加载windows窗体后完成的:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UserWinFormControl.Height = UserWinFormControl.Parent.Height;
UserWinFormControl.Width = UserWinFormControl.Parent.Width;
} 其中,UserWinFormControl是要由WindowsFormHost在Xaml写入中嵌入/托管的控件:
<Window x:Class="myAppWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MyApp"
Height="737"
Width="1024"
WindowStartupLocation="CenterScreen"
Loaded="Window_Loaded">
<......>
<DockPanel LastChildFill="true">
<WindowsFormsHost Name="windowsFormsHost1"DockPanel.Dock="Top"
Background="Yellow"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" >
</WindowsFormsHost>
</DockPanel>
</....>
</Window>一切正常,当调整应用程序大小时没有闪烁。
https://stackoverflow.com/questions/977729
复制相似问题