Click here to Skip to main content
16,019,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, guys.
I have Grid with "star sizing" and expected my buttons to grow or shrink when displaying on bigger or larger screen. I tried to find solution to this but failed.
I thougt that a star in row and column definition sorts everything out but not.
Here is the sample of my code:
<Grid removed="#FF075B16" >
     <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="*" />
     </Grid.RowDefinitions>
     <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="20"/>
         <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <Button x:Name="vitaminsButton" HorizontalAlignment="Left"    Margin="200,140,0,0" VerticalAlignment="Top"
             Height="178" Width="405" Content="Button1" FontSize="24">
         <Button.Background>
             <ImageBrush ImageSource="Assets/001 (www.cute-pictures.blogspot.com).jpg" Stretch="UniformToFill" />
         </Button.Background>
     </Button>
     <Button HorizontalAlignment="Right" Margin="0,140,200,0" VerticalAlignment="Top" Grid.Column="2"
             Height="178" Width="405" Content="Button2" FontSize="24" x:Name="mineralsButton">
         <Button.Background>
             <ImageBrush ImageSource="Assets/1280px-ARS_copper_rich_foods.jpg" Stretch="UniformToFill" />
         </Button.Background>
     </Button>
     <Button HorizontalAlignment="Left" Margin="200,0,0,140" VerticalAlignment="Bottom" Grid.Row="1" x:Name="BrowseAllButton"
             Height="178" Width="405" Content="Button3" FontSize="24">
         <Button.Background>
             <ImageBrush ImageSource="Assets/freshvegetablesinbasket.jpg" Stretch="UniformToFill" />
         </Button.Background>
     </Button>
     <SearchBox Grid.Column="2" HorizontalAlignment="Left" Margin="150,179,0,0" Grid.Row="1"
                VerticalAlignment="Top" Width="357"  PlaceholderText="Search"
                />
 </Grid>
Posted

1 solution

1.place your buttons in separate cells. That means create a cell for each control(button).

2.Then set your control/button's "HorizontalAlignment" and "VerticalAlignment" = "Stretch".

3.Done. Whenever the size of the cell will grow, the button's size will automatically grow.

For example : Window's size is 600x400
HTML
<grid horizontalalignment="Stretch" verticalalignment="Stretch">
    <grid.rowdefinition>
        <rowdefiniton height="*" />
        <rowdefiniton height="*" />
    </grid.rowdefinition>
    <grid.columndefinition>
        <columndefinition width="*" />
        <columndefinition width="*" />
    </grid.columndefinition>

    <button grid.row="0" grid.column="0" horizontalalignment="Stretch" verticalalignment="Stretch" />
    <Button Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <Button Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    <Button Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</grid>

This will stretch your grid to available width and height.

So, now your grid's size is 600x400.

Now You have 4 cells, all having equal sizes.
That means each cell's size is 300x200.

And because we have set each button's alignment property, the buttons will fill to the cell's size.
 
Share this answer
 
Comments
Reatellino 12-Mar-15 7:25am    
OK I get it.:) So, I don't set the height and with of the buttons , I just set the margins and they grow or shrink hehe. Thank You.

BTW Do You know any book I could learn Xaml from basics to advance level?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900