如何将字体大小绑定到可变的网格大小

4
我有一个2列2行的网格。在右下角的网格中放置了单个字符(Unicode U+2699)。它看起来像这样:Grid currently 我希望该字符能够自动调整字体大小以适应所放置的网格字段(在本例中,应绑定到第二个网格行的高度,但由于在某些情况下可能不清楚网格的高度或宽度哪个更小,因此也需要知道如何绑定到这两个值中较小的那个)。
到目前为止,我的实现类似于以下内容(我为此示例简化了它):
<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" 但没有效果)。

2个回答

5
你可以尝试使用 ViewBox 作为按钮的内容:
<Button Grid.Row="1" Grid.Column="1">
    <Button.Content>
        <Viewbox  StretchDirection="Both" HorizontalAlignment="Stretch">
            <TextBlock Text="&#x2699;" />
        </Viewbox>
    </Button.Content>
</Button>

一个ViewBox可以拉伸和缩放它的子元素以填充所有可用的空间...

1

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

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

这应该可以工作。
网格定义中的*表示将可用空间作为高度,因此仅在页面布局准备好进行布局时才确定。如果高度未设置或更改,则实际高度将在ActualHeight属性中返回。

我也尝试了你的解决方案,但字符没有像使用Blachshma的答案那样使用整个行的高度。无论如何,谢谢你的回答! - Samuel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接