3

I'm trying to make a animation in WPF using data bindings. I 'm using MatrixAnimationUsingPath to let a shape follow a path. the path are representet in my viewModel as a array; Point[]. How can I bind my point property in my viwmodel so I can use it with 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> 

I have been able to create a path from the points, using a value converter, but I'm not able to use the path in the MatrixAnimationUsingPath.

<Path Name="MyPath" StrokeThickness="2" Data="{Binding Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}">

Added after comment:

I havn't workt so mush with value converters berfore. the converter I used, I did find online. Hos can I modify it?

[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
}
user2590683
  • 163
  • 1
  • 1
  • 9

2 Answers2

0

You are almost there ... you need a converter that return PathFigure instead of points.

If you Modify the converter the code should work.

Hope it helps.

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • I havn't worked so much with value converters before. The converter I used, I did find online. Hos can I modify it? Se code for value converter I added above – user2590683 Oct 30 '13 at 11:39
  • Can I just return "figure", instead of "geometry". Because, that doesn't work – user2590683 Oct 30 '13 at 11:41
0

Without having tested it: You should be able to reuse the Path.Data binding expression like this:

<MatrixAnimationUsingPath ...
    PathGeometry="{Binding Path=Points,
                   Converter={StaticResource ResourceKey=PointsToPathConverter}}" />

I'm not sure however if you may need to explicitly set the binding source object, as the MatrixAnimationUsingPath object has no DataContext.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I have know tried using what you sad. I'm not getting any errors, but no animation happends. What did you mean with the last sentence? maybe that is the problem.. – user2590683 Oct 30 '13 at 11:53
  • Set the binding `Source` (or `RelativeSource` or `ElementName`) property to the object that has the `Points` property. But you should have seen a binding error message in the Output Window in Visual Studio. – Clemens Oct 30 '13 at 11:58
  • As this: PathGeometry="{Binding Path=Points, Source=ViewModel, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=PointsToPathConverter}}" ?? Because, it doesn't make any difference. And there are no errors! – user2590683 Oct 30 '13 at 12:04
  • I guess `Source=ViewModel` won't work. It might be `Source={StaticResource ViewModel}` in case there is a view model resource and "ViewModel" is its key. But I can't tell that as I don't know your code. You may however read up on data binding in the [Data Binding Overview](http://msdn.microsoft.com/en-us/library/ms752347.aspx) article on MSDN. – Clemens Oct 30 '13 at 12:16