Naftis
Naftis

Reputation: 4559

Letting a child control width size to fit and limiting its max value

Edit: I try rephrasing my question, sorry if it was not clear. Thanks to all anyway.

Say I have a UserControl whose layout has a grid with 1 row x 3 columns, the first 2 autosized and the third star-sized:

<Grid>
<Grid.RowDefinitions>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <ComboBox MinWidth="80" MaxWidth="150" .../>
    <CheckBox Grid.Column="1" VerticalAlignment="Top".../>
    <TextBox Grid.Column="2" MaxHeight="400" TextWrapping="Wrap" HorizontalAlignment="Stretch"...>
</Grid>

The TextBox has text wrapping and its vertical scrollbar visibility set to auto and its horizontal one hidden.

Now, I have some window including a ListBox whose items are instances of this UserControl, like:

<ListBox HorizontalContentAlignment="Stretch".../>

The ListBox is in a 1-column star-sized grid and thus stretches to fit all the available width in its container. When I resize the container of this ListBox the ListBox too resizes as expected as it is stretched in a star-sized Grid column; and the same holds true for the ListBox items', which too are stretched and in this case happen to be instances of a UserControl with the above layout (3 columns in a grid, the 3rd star-sized).

The problem is that the TextBox in the 3rd column of the UserControl used as a listbox item should not automatically increase its width when I type into it some long text: it should just wrap, increasing its height (up to a maximum height; then the vertical scrollbar will appear). In other words, its MaxWidth should be determined by the available space, like its Width.

How should I code my XAML for this?

Upvotes: 1

Views: 4784

Answers (1)

Rachel
Rachel

Reputation: 132648

That should be the behavior by default, based on the code you've posted.

Try setting your TextBox's HorizontalAlignment="Stretch"

Upvotes: 0

Related Questions