views:

1041

answers:

2

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?

+1  A: 

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

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.

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

David Basarab
+1  A: 

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 HorizontalAlignement="Stretch" VerticalAlignement="Stretch">
        <Panel Dock=Fill />
    </WindowsFormsHost>
</DockPanel>

Enjoy',

ykatchou