Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set XAML Width in percentage?

I am trying to create a button in XAML with a 80% width, but I can't seem to figure out how. It's apparently not as easy as using Width="80%". I have been thinking this can be done by detecting the screen width somehow and multiply that by 0.8 and use that as the width, but I am not sure how I can do this in XAML. Perhaps this has to be done in the .cs file and then adjust the width from there. Does anyone have a solution for this?

like image 320
Tom Avatar asked Apr 03 '13 10:04

Tom


People also ask

How to give width in percentage in WPF?

Manage Percentage In HTML, the percentage (%) symbol is used to define a uniform layout that keeps the same width and height ratio when a web page is resized. We do not have this feature in Windows Forms. However, WPF supports this feature in a different manner using an asterisk (*) suffix with a double number.

What is Grid in XAML?

In XAML a Grid is made up of a series of rows and columns. By specifying the row and column of an element within a Grid, you can place and space other elements within a user interface. Rows and columns are defined with the RowDefinition and ColumnDefinition elements.


2 Answers

Is it WPF?

If yes, then wrap your control (button) in grid. Then specify the grid column definition. Example:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.2*"></ColumnDefinition>
    <ColumnDefinition Width="0.8*"></ColumnDefinition>
  </Grid.ColumnDefinitions>
  <Button Grid.Column="1" Grid.Row="0"></Button>
</Grid>

Edit: Forget to close <Button> tag.

like image 116
Fendy Avatar answered Sep 21 '22 19:09

Fendy


I think the more proper way would be

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="2*"></ColumnDefinition>
    <ColumnDefinition Width="10*"></ColumnDefinition>
  </Grid.ColumnDefinitions>
  <Button Grid.Column="1" Grid.Row="0"></Button>
</Grid>

12 grid distribution like bootstrap, it's just about your preference

like image 45
Sheraz Ahmed Avatar answered Sep 25 '22 19:09

Sheraz Ahmed