Skip to main content

Microsoft Silverlight

Answered Question Making control size the same as gridRSS Feed

(0)

gp100mk
gp100mk

Member

Member

7 points

58 Posts

Making control size the same as grid

I'm new to Silverlight and am trying to use the following xaml to make make my StackPanel take the same width as the containing Grid:-

 <Grid x:Name="LayoutRoot" Width="200">

<Border BorderThickness="1" BorderBrush="Black"/>

 

<StackPanel x:Name="menu" HorizontalAlignment="Left" Width="Auto">

<Button x:Name="expandbutton" Content="Menu 1" Click="expandbutton_Click" Width="Auto"/>

<Button Style="{StaticResource ImageButton}" Content="images/home_32.png" Tag="Item 1" Margin="0,10,0,10"/>

<Button Style="{StaticResource ImageButton}" Content="images/cal_32.png" Tag="Item 2" Margin="0,10,0,10"/>

<Button Style="{StaticResource ImageButton}" Content="images/web_32.png" Tag="Item 3" Margin="0,10,0,10"/>

</StackPanel>

</Grid>

In the above example my grid is of Width 200, and the StackPanel has its wdth set to Auto. However, this does not size to the width of the grid as expected. How can this be done? Eventually I want this to be part of a UserControl, so the width of 200 will not be hard-coded.

 

pkr2000
pkr2000

Participant

Participant

1219 points

377 Posts

Re: Making control size the same as grid

 I believe it's because a grid contents are held in rows and columns and not in a simple child way that your code is hoping for. E.g. if you change Grid to Canvas your code will probably work. Other than that you need to specify the row your child goes in then you do tricks like col/row spans to virtually ignore the rows/cols.

gp100mk
gp100mk

Member

Member

7 points

58 Posts

Answered Question

Re: Re: Making control size the same as grid

I managed to get this to work by setting the following on the StackPanel:-

HorizontalAlignment="Stretch"

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities