3

データ バインディングを使用して WPF でアニメーションを作成しようとしています。私は MatrixAnimationUsingPath を使用して、形状がパスをたどるようにしています。パスは、viewModel で配列として表されます。点[]。ポイント プロパティを viwmodel にバインドして、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
}
4

2 に答える 2

0

PathFigureあなたはもうすぐそこにいます...ポイントの代わりに返すコンバーターが必要です。

コンバーターを変更すると、コードが機能するはずです。

それが役に立てば幸い。

于 2013-10-30T11:31:55.113 に答える
0

テストしていなくても、次のように Path.Data バインディング式を再利用できるはずです。

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

ただし、MatrixAnimationUsingPath オブジェクトには DataContext がないため、バインディング ソース オブジェクトを明示的に設定する必要があるかどうかはわかりません。

于 2013-10-30T11:38:44.397 に答える