damitha119
damitha119

Reputation: 41

How to use percentages on sizing in WPF components?

I was using WPF rectangles and need to re size when re-sizing the window. I tried several helps posted several places like using the star("*") after the size but didn't work. Any help regarding this matter?

 <StackPanel>
   <Rectangle Height="5">
   <Rectangle Height="495">
     <Rectangle.Fill>
       <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
         <GradientStop Color="#F7F6EF" Offset="0.0"></GradientStop>
         <GradientStop Color="#E5DABE" Offset="1.0"></GradientStop>
       </LinearGradientBrush>
     </Rectangle.Fill>
   </Rectangle>
 </StackPanel>

Upvotes: 3

Views: 952

Answers (3)

Rhyous
Rhyous

Reputation: 6690

Just change StackPanel to DockPanel

Upvotes: 1

rlcrews
rlcrews

Reputation: 3562

If you want the inner rectangle to be bound to the sides of the grid and scale proportionality use horizontalalign and verticalalign. This will "pin" the sides of the child element to the parent container.

<Grid.ColumnDefinitions>
   <ColumnDefinition MinWidth="1*"></ColumnDefinition>
   <ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Column="0" HorizontalAlign="stretch" VerticalAlign="stretch"/>

Using the star uses an internal algorithm that divides the element based upon the set value ( 1* and 2* would be the equivalent of column one occupying 1/3 of the screen and the second column occupying 2/3 of the screen)

Upvotes: 0

Nathan
Nathan

Reputation: 6531

Size a grid and it's child elements and then put it inside a viewbox. It will all expand and contract nicely.

<Viewbox Stretch="Uniform" MinHeight="700" MinWidth="258">   
<Grid Height="700" Width="250">
<Grid Width="235" Margin="0,15,0,0" Height="620">
</Grid>
</Grid>
</Viewbox>

that sort of thing you can even create nice things like forcing the stretch to be uniform, which is what i think you are after.

Bear in mind you need this at the top in :

ResizeMode="CanResizeWithGrip"

Obviously it doesn't need to be with grip, just not false, but i like grip. edit: i think it's true by default.

Note that view boxes can only have one child so be sure to still have a grid/canvas etc to draw the rectangles just like normal. Also it can screw up if you try to be clever with setting the size of the grid within the view-box dynamically.

Upvotes: 1

Related Questions