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?
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',
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With