views:

322

answers:

1

I am having hard times figuring this out. Here is what I need:

<StackPanel x:Name="container" VerticalAlignment="Stretch">
     <RichTextBox Height="???" />
</StackPanel>

Basically what I know I can do is to bind RichTextBox Height to it's parent's height ( Height="{Binding ElementName=container, Path=ActualHeight}". Unfortunately this only works on load, because as it seems ActualHeight and ActualWidth don't notify for changes.

So what is the best way in Silverlight 4 to tell RichTextBox or TextBlock, it doesn't matter, to fill it's parent height, and maintain scrollbar if it's content height is bigger. Is the only way to bind some Resize events and maintain the height explicitly? That seems really ugly to me? Have anybody had this problem as well?

Any resources or information is highly appreciated! Thanks.

A: 

Ivan,

The best way to solve this is to use a Grid as the parent for the RickTextBox, instead of a StackPanel. By default, a Grid will "Strectch" its content to take up all of the available space. A StackPanel will only Stretch its content in one diminsion.

As an example, paste the following XAML into my XamlViewer to see the difference:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <RichTextBox
                Foreground="Blue" FontSize="24" Background="Yellow">
                <Paragraph>RichTextBox inside a StackPanel</Paragraph>
            </RichTextBox>
        </StackPanel>
        <Grid Grid.Row="1">
            <RichTextBox
                Foreground="Blue" FontSize="24" Background="Tan">
                <Paragraph>RichTextBox inside a Grid</Paragraph>
            </RichTextBox>
        </Grid>
    </Grid>
</UserControl>

Good luck,
Jim McCurdy, Face to Face Software and YinYangMoney

Jim McCurdy
Doesn't help solve the actual problem. When the RichTextBox Height isn't explicitly set, it will resize to fit it's content and will resize ( if content is bigger than the container ) the parent, no matter what you set.
Ivan Zlatanov
Maybe I wasn't clear. You shouldn't have to bind the height. If the RichTextBox is placed inside a Grid, it will maintain the same size as the Grid because a Grid will stretch its content by default. A StackPanel will only Stretch its content in one dimension. I edited my answer above to clarify.
Jim McCurdy
I totally got that the first time. :) The problem isn't that. Start typing into the RichTextBox until it starts to scroll. The problem is that it never doesn't and resizes the height of itself and it's parent.
Ivan Zlatanov
Hm. I actually got this to work in an empty project inside of a grid. So my guess is that another thing is causing my problems. Anyway, your answer is correct and thus I will accept it.
Ivan Zlatanov