我试图使用数据绑定在WPF中制作动画。我使用MatrixAnimationUsingPath让形状遵循路径。路径在我的viewModel中表示为数组;Point[]。我如何在viwmodel中绑定我的point属性,以便我可以将它与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> 我已经能够使用值转换器从点创建路径,但是我不能使用MatrixAnimationUsingPath中的路径。
<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
}发布于 2013-10-30 11:31:55
你快到了..。您需要一个返回PathFigure而不是点的转换器。
如果您修改转换器,代码应该可以工作。
希望能帮上忙。
发布于 2013-10-30 11:38:44
无需测试它:您应该能够像这样重用Path.Data绑定表达式:
<MatrixAnimationUsingPath ...
PathGeometry="{Binding Path=Points,
Converter={StaticResource ResourceKey=PointsToPathConverter}}" />但是,我不确定您是否需要显式地设置绑定源对象,因为MatrixAnimationUsingPath对象没有DataContext。
https://stackoverflow.com/questions/19680947
复制相似问题