我正在尝试使 a 居中Path
,使其原点 (0, 0) 位于其容器的底部中心。假设容器是一个Grid
.
例子:
注意:箭头的尾部在原点 (0, 0)。尾巴水平居中,但整体箭头向左倾斜。无论箭头指向哪个方向,这都是我想要实现的。
这需要适用于 x 和 y 坐标为正、负或两者混合的路径。
这如何通过 XAML 以最少的标记来完成?
我正在尝试使 a 居中Path
,使其原点 (0, 0) 位于其容器的底部中心。假设容器是一个Grid
.
例子:
注意:箭头的尾部在原点 (0, 0)。尾巴水平居中,但整体箭头向左倾斜。无论箭头指向哪个方向,这都是我想要实现的。
这需要适用于 x 和 y 坐标为正、负或两者混合的路径。
这如何通过 XAML 以最少的标记来完成?
这是我一直在使用的。只需在您想要的任何地方绘制您的路径,然后将其转换为所需的起点。您可以使用绑定使其在网格中居中。这允许将几何图形放置在网格中的任何位置。
<Grid>
<Path Stroke="Black"
StrokeThickness="1">
<Path.Data>
<PathGeometry>
<!-- Your path geomrtry -->
</PathGeometry>
</Path.Data>
<Path.RenderTransform>
<TranslateTransform Y="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=ActualHeight, Converter={StaticResource widthAndHeightDivider}}"
X="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=ActualWidth, Converter={StaticResource widthAndHeightDivider}}"/>
</Path.RenderTransform>
</path>
</Grid>
并具有以下转换器,它将网格的 ActualWidth 除以使其居中:
public class WidthAndHeightDivider : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double d = (double)value / 2.0;
return d;
}
}
我希望这有帮助!
这就是我最终的结果。似乎有效,但如果有更清洁的方法可以做到这一点,我很想听听:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Path Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
StrokeThickness="1"
Data="{Binding PathGeometry}" />
</Grid>
嗯......底部中间的位置取决于容器。尝试这样的事情:
<Grid>
<Path Stroke="Black"
StrokeThickness="1"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Data="M 0,0 L -50,-70 L -50,-80" />
</Grid>