0

我有一个 Grid 控件,我想在 XAML 中设置边距属性并将它们从SystemInformation.CaptionHeight. 我已经想出了如何在 C# 中做到这一点,但我想在 XAML 中做到这一点。

我以前没有使用资源来设置这样的东西,但我尝试过类似的变体

<Grid>
   <Grid.Margin>
      <Thickness Top="{StaticResource {x:Static Forms:SystemInformation.CaptionHeight}}" Left="5" Right="5" Bottom="5" />
   </Grid.Margin>
</Grid>

<Grid>
   <Grid.Margin>
      <Thickness Top="{x:Static Forms:SystemInformation.CaptionHeight}" Left="5" Right="5" Bottom="5" />
   </Grid.Margin>
</Grid>

但无济于事。如何在运行时动态设置这样的属性?

4

1 回答 1

1

使用绑定,但指定源是静态的并将其指向您的属性的位置

另外,Thickness.Top不是 DependencyProperty,所​​以不能绑定它。您最好的选择是Grid.Margin使用转换器进行绑定,该转换器采用双精度值并将其转换为Thickness属性。

<Grid Margin="{Binding 
          Source={x:Static Forms:SystemInformation.CaptionHeight},
          Converter={StaticResource MyDoubleToMarginConverter}}" />
于 2011-12-01T20:10:30.433 回答