我在Grid.Row和Grid.Column中画了一个椭圆。行高始终大于列宽。
我想要画一个填满网格列宽度的椭圆,其高度使其成为一个完美的圆形。
我还想在上述椭圆的正中央精确地画出一个单个数字。基本上是画了一个内部有数字居中的圆。
我可以轻松地在我的Ellipse和包含数字的TextBlock上设置HorizontalAlignment="Stretch"。这为我处理了宽度。然而,当Grid.Column的宽度改变时,如何使Ellipse和TextBlock的高度始终与其宽度匹配呢?
以下是一些XAML来说明这一点。在下面的XAML中,我希望硬编码的'63'基于Grid.Column的宽度或椭圆的Width字段:
<Ellipse Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch" Height="63" Stroke="Black" StrokeThickness="3" VerticalAlignment="Top"/>
<TextBlock Grid.Row="1" Grid.Column="0" Width="63" Height="63" VerticalAlignment="Top" Text="1" TextAlignment="Center" FontSize="42" FontWeight="Bold"/>
感谢你的帮助。最终我使用了 Herdo 的答案,只需将 HorizontalAlignment 设置为 Stretch,然后将高度绑定到椭圆的实际宽度即可。我对椭圆和文本块都进行了相同的操作:
<Ellipse HorizontalAlignment="Stretch" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"/>
<TextBlock HorizontalAlignment="Stretch" Height="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"/>