WPF - Margin in percentage

与世无争的帅哥 提交于 2019-11-30 21:13:06

Here's how you implement 20% left and right margins in WPF:

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="2*"/>
      <ColumnDefinition Width="6*"/>
      <ColumnDefinition Width="2*"/>
   </Grid.ColumnDefinitions>
   <TextBlock Grid.Column="1" Text="Hello, world."/>
</Grid>

This may seem ridiculously verbose if what you're trying to do is re-implement a simple CSS layout in WPF, but that's because implementing simple CSS layouts is really not the problem space WPF is designed around.

Instead of using Margin, you can do this via a Grid.

Just place your control within a Grid element, and use 3 columns and 3 rows. The column/row sizing can be done as percentages of the containing element.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!