首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF WindowsFormsHost大小调整

WPF WindowsFormsHost大小调整
EN

Stack Overflow用户
提问于 2009-06-10 19:38:26
回答 5查看 25.1K关注 0票数 17

我想要将windows窗体控件包装在wpf UserControl中。

代码语言:javascript
运行
复制
<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,基本上就消失了。然后用户需要调整窗口的大小(缩小是因为用户控件说我不需要空间,谢谢)。当用户调整大小时,它会伸展到用户指定的任何位置。

所以我的问题是我做错了什么?如何让我的用户控件占用所有可用的空间,而不是不请求任何空间?

EN

回答 5

Stack Overflow用户

发布于 2010-09-21 20:26:54

我找到了一个更好的答案。

将dockPanel与LastChildFill=true一起使用,然后将具有水平和垂直对齐的WindowsFormsHost放入Strech,当然WindowsForms控件也必须填充。

代码语言:javascript
运行
复制
<DockPanel LastChildFill="true">
    <WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Panel Dock=Fill />
    </WindowsFormsHost>
</DockPanel>

享受‘,

票数 21
EN

Stack Overflow用户

发布于 2014-05-13 02:10:32

一些更正:默认情况下,DockPanel已启用LastChildFill,水平路线和垂直路线也会自动设置为拉伸。

因此,简洁的答案是:使用DockPanel

代码语言:javascript
运行
复制
<DockPanel>
     <WindowsFormsHost>
     </WindowsFormsHost>
</DockPanel>
票数 6
EN

Stack Overflow用户

发布于 2012-01-18 18:55:38

这里也有同样的问题。我所做的是将WindowsForm控件(必须嵌入)维度绑定到父主机(即WindowsFormHost)的维度(宽度和高度)。请参阅下面的代码。这是在加载windows窗体后完成的:

代码语言:javascript
运行
复制
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
    UserWinFormControl.Height = UserWinFormControl.Parent.Height;
    UserWinFormControl.Width = UserWinFormControl.Parent.Width;

 }   

其中,UserWinFormControl是要由WindowsFormHost在Xaml写入中嵌入/托管的控件:

代码语言:javascript
运行
复制
<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>

一切正常,当调整应用程序大小时没有闪烁。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/977729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档