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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With