简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM