WPF WindowsForms 主机大小调整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:32:58  来源:igfitidea点击:

WPF WindowsFormsHost sizing

wpfuser-controlswindowsformshost

提问by Jose

I wanted to wrap a windows forms control in a wpf UserControl

我想在 wpf UserControl 中包装一个 windows 窗体控件

<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>

Notice that Height and Width are auto.

请注意,高度和宽度是自动的。

When I have it in a stackpanel or grid control it sets its height to 0 and basically disappears. The user is then required to resize the window(which shrunk because the usercontrol said I don't need no space, thanks). When the user resizes it stretches to whatever the user specifies.

当我将它放在堆栈面板或网格控件中时,它会将其高度设置为 0 并基本上消失。然后用户需要调整窗口大小(因为用户控件说我不需要空间,所以缩小了,谢谢)。当用户调整大小时,它会拉伸到用户指定的任何内容。

So my question is what did I do wrong? How do I make my usercontrol take all the space available instead of not asking for any?

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

回答by ykatchou

I find a better answer.

我找到了更好的答案。

Use a dockPanel with LastChildFill=true and then put inside the WindowsFormsHost with Horizontal and Vertical Alignement to Strech, and of course the WindowsForms control have to fill.

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

<DockPanel LastChildFill="true">
    <WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Panel Dock=Fill />
    </WindowsFormsHost>
</DockPanel>

Enjoy',

享受',

回答by DLeh

Some corrections: DockPanel has LastChildFill enabled by default, and Horizontal and Vertical alignments are also automatically set to stretch.

一些更正:DockPanel 默认启用 LastChildFill,水平和垂直对齐也自动设置为拉伸。

So the concise answer is: Use a DockPanel

所以简洁的答案是:使用 DockPanel

<DockPanel>
     <WindowsFormsHost>
     </WindowsFormsHost>
</DockPanel>

回答by alexander lilov

The same problem here. What I have done is to bind the WindowsForm control (that must be embeded) dimensions to the dimensions (Width and Height) of the parent Host(i.e. the WindowsFormHost) see the code below. This is done after the windows form is loaded:

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

 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
    UserWinFormControl.Height = UserWinFormControl.Parent.Height;
    UserWinFormControl.Width = UserWinFormControl.Parent.Width;

 }   

Where UserWinFormControl is the control to be embeded/hosted by the WindowsFormHost In the Xaml write:

其中 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>

All is working fine no flickers when resizing the app.

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

回答by David Basarab

I had this same problem. The way I fixed it was to change the size of the control inside the WindowsFormsHost at runtime.

我有同样的问题。我修复它的方法是在运行时更改 WindowsFormsHost 内控件的大小。

In my case I used a StackPanel to host the control and at runtime I set the Height and Width of the control inside my WindowsFormsHost to the height and width of the Stackpanel. You want to use the ActualHieight and ActualWidth properties.

就我而言,我使用 StackPanel 来承载控件,并在运行时将 WindowsFormsHost 中控件的高度和宽度设置为 Stackpanel 的高度和宽度。您想要使用 ActualHieight 和 ActualWidth 属性。

Unfortunately I had to hook up to the sizing event change it each time the window was resized.

不幸的是,每次调整窗口大小时,我都必须连接到大小事件来更改它。

回答by thepaulpage

I had some sort of similar problem. I was able to fix my issue by removing the min and max Height and Width settings from the winForm form.

我有一些类似的问题。我能够通过从 winForm 表单中删除最小和最大高度和宽度设置来解决我的问题。

After that I used the DockPanel like ykatchou suggested. I still have some issues with DPI but I don't think they'll matter much. It's ugly, but it's working

之后,我像 ykatchou 建议的那样使用了 DockPanel。我仍然对 DPI 有一些问题,但我认为它们不会有太大影响。这很丑陋,但它正在工作