Vlad
Vlad

Reputation: 1797

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?

Upvotes: 28

Views: 49762

Answers (2)

Sheraz Ahmed
Sheraz Ahmed

Reputation: 899

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

Upvotes: 7

Fendy
Fendy

Reputation: 4643

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.

Upvotes: 62

Related Questions