简体   繁体   中英

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

    <WrapPanel Height="200" VerticalAlignment="Top" Orientation="Vertical">
        <Button x:Name="BookCover" Click="ChangeImageFile" Margin="10">
            button style and content
        </Button>
        <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>
        </WrapPanel>
    </WrapPanel>

How can I make it fit inside the window?

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

EDIT I am including a solution to fix your issue. The method is to use a Grid inside parent wrappanel to equaly divide space between button and content into 2 equal columns. Check if this helps.

<WrapPanel Height="200"
           VerticalAlignment="Top"
           Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Button x:Name="BookCover"
                Grid.Column="0"
                Margin="10"
                Click="ChangeImageFile">
            button style and content
        </Button>
        <WrapPanel Grid.Column="1" Margin="10">
            <TextBox Text="New Book" />
            <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" />
        </WrapPanel>
    </Grid>
</WrapPanel>

As I said in the comment, you should be good by using Grid something like:

<Window ...>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/> <!--to fit to the content-->
            <ColumnDefinition Width="*"/> <!--fill all the remaining-->
        </Grid.ColumnDefinitions>
        <Button x:Name="BookCover" Click="ChangeImageFile" Margin="10">
            button style and content
        </Button>
        <StackPanel Grid.Column="1" 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>
        </StackPanel>
    </Grid>    
</Window>

Result: 结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM