Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF WindowsFormsHost sizing

I wanted to wrap a windows forms control in a 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>

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.

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?

like image 973
Jose Avatar asked Jun 10 '09 19:06

Jose


2 Answers

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.

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

Enjoy',

like image 187
ykatchou Avatar answered Sep 17 '22 12:09

ykatchou


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

So the concise answer is: Use a DockPanel

<DockPanel>
     <WindowsFormsHost>
     </WindowsFormsHost>
</DockPanel>
like image 29
DLeh Avatar answered Sep 19 '22 12:09

DLeh