WPF - 保证金百分比 [英] WPF - Margin in percentage
问题描述
我刚刚开始使用 WPF.从那以后,我开始关注造型系统.我来自 CSS 背景,我想以百分比设置边距.
I have just started using WPF. I'm getting my head around styling system since. I come from a CSS background and I would like to set margin in percentage.
<Style TargetType="TextBlock" x:Key="workflowNameTextBlock">
<Setter Property="Margin" Value="50"/>
</Style>
当前值以像素为单位设置,但我想将其设置为 %,即 50%.
Currently value is set in pixels, but I would like to set it in %, i.e. 50%.
我怎样才能做到这一点?
How can I achive this?
谢谢
推荐答案
以下是在 WPF 中实现 20% 左右边距的方法:
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>
如果您尝试在 WPF 中重新实现一个简单的 CSS 布局,这可能看起来冗长可笑,但那是因为实现简单的 CSS 布局实际上并不是 WPF 设计的问题空间.
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.
这篇关于WPF - 保证金百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!