4

我有一个 2 列 2 行的网格。一个单一的字符 (Unicode U+2699) 被放置在右下角的网格区域内。它看起来像这样:

目前网格

我希望角色自动调整其字体大小以适应它所在的网格字段(在这种情况下,它应该绑定到第二个网格行的高度,但由于在某些情况下可能不清楚网格的高度或宽度更小,知道如何绑定到这两个值中的最低值也很好)。

到目前为止,我的实现类似于以下内容(我在此示例中对其进行了一些简化):

<Grid>
   <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition x:Name="heightToBind" Height="40"/>
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="14*" />
      <ColumnDefinition Width="*" />
   </Grid.ColumnDefinitions>

   <Button FontSize="{Binding ElementName=heightToBind, Path=Height.Value, Mode=OneWay}" Content="&#x2699;" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" />
</Grid>

这里的问题是,它只有在高度是RowDefinitions 中的固定值时才有效。我希望它使用以下定义:

<Grid.RowDefinitions>
   <RowDefinition Height="4*"/>
   <RowDefinition x:Name="heightToBind" Height="*"/>
</Grid.RowDefinitions>

作为一个额外的问题,我也很感兴趣,为什么角色放置得太低以至于它在底部被切断(我尝试VerticalAlignment="Center"了按钮但没有效果)。

4

2 回答 2

5

您可以尝试使用 aViewBox作为按钮的内容:

<Button Grid.Row="1" Grid.Column="1">
    <Button.Content>
        <Viewbox  StretchDirection="Both" HorizontalAlignment="Stretch">
            <TextBlock Text="&#x2699;" />
        </Viewbox>
    </Button.Content>
</Button>

ViewBox可以拉伸和缩放他的孩子以填充所有可用空间......

于 2012-12-09T12:51:50.313 回答
1

您可以尝试绑定到ActualHeight而不是Height

<Button FontSize="{Binding ElementName=heightToBind, Path=ActualHeight.Value, Mode=OneWay}"
        Content="&#x2699;" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" />

这应该有效。

网格上的*定义意味着将可用空间作为高度,因此只有在页面布局准备好布局时才确定。如果高度未设置或更改,则在ActualHeight属性中返回实际高度。

于 2012-12-09T12:41:45.297 回答