Andrei Ilie Andrei Ilie - 1 year ago 120
C# Question

WrapPanel child taking full width of parent

I have two elements inside a WrapPanel, a button and another WrapPanel. The button has a dynamic width. The issue is that the child WrapPanel assumes the full width of the parent WrapPanel, overflowing to the right because of the Button on its left.

WrapPanel overflowing

<WrapPanel Height="200" VerticalAlignment="Top" Orientation="Vertical">
<Button x:Name="BookCover" Click="ChangeImageFile" Margin="10">
button style and content
<WrapPanel Margin="10">
<TextBox Text="New Book"></TextBox>
<TextBox Text="Lorem ipsum dolor sit amet, eos iusto graece consetetur cu, an dicam nonumy volutpat qui, usu at nullam alienum. Est suas autem augue ad, integre vituperatoribus quo id, delicata gloriatur pro ei. Vel at nisl decore, ut eum altera commune. Nec at agam menandri, putant insolens suavitate ei mei, ea possim veritus mea. Nec nemore iuvaret detracto at, quodsi consectetuer pri id. Integre suscipit voluptua no pro, nonumes incorrupte ut mea. Delenit democritum vel in, quem tale everti vim id, id vis melius necessitatibus. Paulo signiferumque per te. Cu mei modus exerci partiendo, ad iudico adolescens mea, et stet integre civibus mea." TextWrapping="Wrap"></TextBox>

How can I make it fit inside the window?

Answer Source

The issue is probably caused by Orientation="Vertical". As per MSDN, If the Orientation property is set to Horizontal, child content forms horizontal rows first and if necessary forms vertical stacks of rows. If the Orientation property is set to Vertical, child content is first positioned in a vertical column, and if there is not enough space, wrapping occurs and additional columns in the horizontal dimension are added.

Here you have set the Orientation to Vertical and has not set a default width to the Wrappanel. This causes the issue. The wrapping occurs vertically and causes the child content to move out of the visible area. Either define a constant width to the wrappanel or fit to the window width like : Width="{Binding ElementName=Window, Path=Width}" or try consider removing the Orientation="Vertical".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download