Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Can i set the width of an element by percentages?

Tags:

Say I have 2 buttons in an Element and I want to set the 2 elements to always fill up 1/2 width of its containing element each, can i do that?

UPDATE

why cant i do something like

<StackPanel Orientation="Horizontal" Grid.Row="0">     <Button Content="Click me" Command="{Binding ClickCommand}" Width="1*" />     <Button Content="Exit" Command="{Binding CloseCommand}" Width="1*" /> </StackPanel> 

why doesnt the 1* work in this context? i get the error

Cannot convert "1*"

like image 799
Jiew Meng Avatar asked Sep 26 '10 00:09

Jiew Meng


1 Answers

You can use a Grid with two columns for this.

<Grid>   <Grid.ColumnDefinitions>     <ColumnDefinition Width="1*"/>     <ColumnDefinition Width="1*"/>   </Grid.ColumnDefinitions>    <Button Grid.Column="0">Button1</Button>   <Button Grid.Column="1">Button2</Button> </Grid> 

Notice the use of star(*) in the ColumnDefinition.Width property. This means that both columns will take up the same amount of space. So in the example above, each button will each occupy 1/2 of the available space of the containing Grid. So if you make one Width to be equal to 2*, that column will take up twice the amount of space as the other column. Hope this makes sense.

like image 91
ASanch Avatar answered Oct 26 '22 21:10

ASanch