tags:

views:

12

answers:

2

Guys,

I need your help! :-)

Check out this code example:

    <ListBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="500">
        <ListBox x:Name="Item1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300" BorderBrush="Red" BorderThickness="5">
            <ListBox.Items>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300" SizeChanged="OnInnserItemSizeChanged"/>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300"/>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300"/>
            </ListBox.Items>
        </ListBox>
        <ListBox x:Name="Item2" BorderBrush="Green" BorderThickness="5" Width="300" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <ListBox.Items>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300"/>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300"/>
                <RichTextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="300"/>
            </ListBox.Items>
        </ListBox>            
    </ListBox>

Now, when i run it and click 'Enter'/'Return' in one of the RichTextBoxes, the outer listbox item (for example x:Name=Item1) stretches to match the new size - but when i delete the newly added row it doesn't resize back to its original size...

Thanx, Gili

+1  A: 

This is caused by the way VirtualizingStackPanel handles scrolling, although I don't know the reason behind it. You can prevent it by using a regular StackPanel as the ItemsPanel instead of a VirtualizingStackPanel:

<ListBox x:Name="Item1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
         Width="300" BorderBrush="Red" BorderThickness="5">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

or by preventing the VirtualizingStackPanel from handling scrolling by setting ScrollViewer.CanContentScroll to False:

<ListBox x:Name="Item1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
         Width="300" BorderBrush="Red" BorderThickness="5"
         ScrollViewer.CanContentScroll="False">

Note that doing either of these will cause you to lose virtualization in the inner ListBox. In this case you weren't taking advantage of that anyway, since the outer ListBox has a ScrollViewer that will let the inner ListBox grow as high as it wants, which means it will always display all of its rows.

Quartermeister
A: 

Unbelievable!! You're a king!! i've been looking for a solution to this for quite along time.

THANX A LOT!!!

Gilad