C# 如何将点[]转换为PathGeometry?
我正在尝试使用数据绑定在WPF中制作动画。我正在使用MatrixAnimationUsingPath让形状跟随路径。路径在我的viewModel中表示为数组;第[]点。如何在viwmodel中绑定point属性,以便将其与MatrixAnimationUsingPath一起使用C# 如何将点[]转换为PathGeometry?,c#,wpf,xaml,animation,pathgeometry,C#,Wpf,Xaml,Animation,Pathgeometry,我正在尝试使用数据绑定在WPF中制作动画。我正在使用MatrixAnimationUsingPath让形状跟随路径。路径在我的viewModel中表示为数组;第[]点。如何在viwmodel中绑定point属性,以便将其与MatrixAnimationUsingPath一起使用 <Storyboard> <MatrixAnimationUsingPath Storyboard.TargetName="MyMatrixTransform" Storyboard.
<Storyboard>
<MatrixAnimationUsingPath Storyboard.TargetName="MyMatrixTransform"
Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="True"
Duration="0:0:5" RepeatBehavior="Forever">
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry>
// WHAT TO PUT HERE!
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
<Path Name="MyPath" StrokeThickness="2" Data="{Binding Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}">
//在这里放什么!
我已经能够使用值转换器从点创建路径,但无法使用MatrixAnimationUsingPath中的路径
<Storyboard>
<MatrixAnimationUsingPath Storyboard.TargetName="MyMatrixTransform"
Storyboard.TargetProperty="Matrix" DoesRotateWithTangent="True"
Duration="0:0:5" RepeatBehavior="Forever">
<MatrixAnimationUsingPath.PathGeometry>
<PathGeometry>
// WHAT TO PUT HERE!
</PathGeometry>
</MatrixAnimationUsingPath.PathGeometry>
</MatrixAnimationUsingPath>
</Storyboard>
<Path Name="MyPath" StrokeThickness="2" Data="{Binding Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}">
评论后添加: 我以前没有在价值观方面做过这么多工作。我使用的转换器,我在网上找到了。我可以修改它吗
[ValueConversion(typeof(Point[]), typeof(Geometry))]
public class PointsToPathConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Point[] points = (Point[])value;
if (points.Length > 0)
{
Point start = points[0];
List<LineSegment> segments = new List<LineSegment>();
for (int i = 1; i < points.Length; i++)
{
segments.Add(new LineSegment(points[i], true));
}
PathFigure figure = new PathFigure(start, segments, false); //true if closed
PathGeometry geometry = new PathGeometry();
geometry.Figures.Add(figure);
return geometry;
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
[ValueConversion(typeof(Point[])、typeof(Geometry))]
公共类PointsToPathConverter:IValueConverter
{
#区域转换器成员
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
点[]点=(点[])值;
如果(点长度>0)
{
起点=点[0];
列表段=新列表();
对于(int i=1;i
你就快到了。。。您需要返回PathFigure
而不是点的转换器
如果修改转换器,代码应该可以工作
希望对您有所帮助。在没有测试的情况下:您应该能够重用路径。数据绑定表达式如下:
<MatrixAnimationUsingPath ...
PathGeometry="{Binding Path=Points,
Converter={StaticResource ResourceKey=PointsToPathConverter}}" />
但是,我不确定您是否需要显式地将绑定源对象设置为
MatrixAnimationUsingPath对象没有DataContext。我以前没有使用过这么多值转换器。我使用的转换器,我在网上找到了。我可以修改它吗?我在上面添加的值转换器Se代码是否可以返回“figure”,而不是“geometry”。因为,这不起作用,我知道我试过用你伤心的东西。我没有得到任何错误,但没有动画发生。你最后一句话是什么意思?可能这就是问题所在。将绑定
源
(或相对源
或元素名
)属性设置为具有点
属性的对象。但是您应该在Visual Studio的输出窗口中看到绑定错误消息。如下所示:PathGeometry=“{binding Path=Points,Source=ViewModel,UpdateSourceTrigger=PropertyChanged,Converter={StaticResourceKey=PointsToPathConverter}”??因为,这没什么区别。没有错误!我猜Source=ViewModel
不起作用。它可能是Source={StaticResource ViewModel}
如果有一个视图模型资源,“ViewModel”是它的键。但我不知道,因为我不知道你的密码。不过,您可以在MSDN的文章中阅读有关数据绑定的内容。