【发布时间】:2012-03-05 20:36:49
【问题描述】:
我正在尝试将 Path 居中,使其原点 (0, 0) 位于其容器的底部中心。假设容器是Grid。
示例:
注意:箭头的尾部在原点 (0, 0)。尾巴水平居中,但整体箭头向左倾斜。无论箭头指向哪个方向,这都是我想要实现的目标。
这需要适用于 x 和 y 坐标为正、负或两者混合的路径。
如何通过 XAML 以最少的标记来完成这项工作?
【问题讨论】:
标签: wpf xaml geometry pathgeometry
我正在尝试将 Path 居中,使其原点 (0, 0) 位于其容器的底部中心。假设容器是Grid。
示例:
注意:箭头的尾部在原点 (0, 0)。尾巴水平居中,但整体箭头向左倾斜。无论箭头指向哪个方向,这都是我想要实现的目标。
这需要适用于 x 和 y 坐标为正、负或两者混合的路径。
如何通过 XAML 以最少的标记来完成这项工作?
【问题讨论】:
标签: wpf xaml geometry pathgeometry
这是我一直在使用的。只需在您想要的任何地方绘制您的路径,然后将其转换为所需的起点。您可以使用绑定使其在网格中居中。这允许您将几何图形放置在网格中的任何位置。
<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>
【讨论】: