views:

169

answers:

1

I am new to Silverlight/XAML so apologies if this is an obvious question.

How can you detect when the OOB window is resized and resize your own controls to fit the new window size?

+1  A: 

In Silverlight (regardless of OOB or not) you wouldn't normally need to detect window resizing to perform your own resizing. Using the correct panel types sorts that out for you.

For example:-

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 >
 <Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Rectangle Fill="Green" Grid.Row="0" Grid.Column="0" />
    <Rectangle Fill="Red" Grid.Row="0" Grid.Column="1" />
    <Rectangle Fill="Yellow" Grid.Row="1" Grid.Column="0" />
    <Rectangle Fill="Blue" Grid.Row="1" Grid.Column="1" />
</Grid>
</UserControl>

Here the four rectangles divide up the window into quarters. It may be worth your while spending a little time review the documentation for the various panel types such as Grid, Canvas and StackPanel to get a feel for how each works.

AnthonyWJones
Thanks for responding. This is just like the example I was working on. The grid fills the window on startup but does not change when the window is resized as I was hoping it would. Any idea what I am doing wrong?
Paul Dolphin
@pauldolphin: Without sight of you code its difficult to advise. Just check you haven't specified a width and height on the UserControl or other control ancestors of your grid.
AnthonyWJones
Bingo - that was the problem. I had specified a Width and Height for the UserControl.Thanks for your help.
Paul Dolphin