好吧,我不太确定您要做什么,所以这里有几个解决方案。他们都使用Grid
,因为你特别要求它。但是,我不相信它真的合适。
您的问题是Ellipse
大小由 决定Grid
,并且没有强制纵横比的约束。因此,纵横比不仅会在屏幕分辨率不同时发生变化,还会在您调整窗口大小时发生变化。
解决方案1:设置椭圆的宽度和高度
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="257*" />
<RowDefinition Height="121*" />
<RowDefinition Height="442*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="520*" />
<ColumnDefinition Width="121*" />
<ColumnDefinition Width="865*" />
</Grid.ColumnDefinitions>
<Ellipse Stroke="Black" Grid.Row="1" Grid.Column="1" Width="20" Height="20" />
</Grid>
解决方案 2:在 Grid 中设置 Width 和 Height
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="257*" />
<RowDefinition Height="20" />
<RowDefinition Height="442*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="520*" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="865*" />
</Grid.ColumnDefinitions>
<Ellipse Stroke="Black" Grid.Row="1" Grid.Column="1" />
</Grid>
解决方案 3:使用 ViewBox
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="257*" />
<RowDefinition Height="121*" />
<RowDefinition Height="442*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="520*" />
<ColumnDefinition Width="121*" />
<ColumnDefinition Width="865*" />
</Grid.ColumnDefinitions>
<Viewbox Grid.Row="1" Grid.Column="1">
<Ellipse Stroke="Black" Width="20" Height="20" />
</Viewbox>
</Grid>
解决方案4:将椭圆的宽度和高度绑定在一起
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="257*" />
<RowDefinition Height="121*" />
<RowDefinition Height="442*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="520*" />
<ColumnDefinition Width="121*" />
<ColumnDefinition Width="865*" />
</Grid.ColumnDefinitions>
<Ellipse x:Name="ellipse" Stroke="Black" Grid.Row="1" Grid.Column="1"
Height="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}" />
</Grid>
差异
解决方案 1 和 2,无论Grid
大小是多少,都给圆一个恒定的大小。
解决方案 3 和 4,都根据大小调整圆的Grid
大小。
解决方案 3 也会改变笔画的粗细,而解决方案 4 不会。
编辑:固定解决方案 4