首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何转换Point[] til PathGeometry?

如何转换Point[] til PathGeometry?
EN

Stack Overflow用户
提问于 2013-10-30 11:27:45
回答 2查看 2.3K关注 0票数 3

我试图使用数据绑定在WPF中制作动画。我使用MatrixAnimationUsingPath让形状遵循路径。路径在我的viewModel中表示为数组;Point[]。我如何在viwmodel中绑定我的point属性,以便我可以将它与MatrixAnimationUsingPath一起使用。

代码语言:javascript
复制
<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中的路径。

代码语言:javascript
复制
<Path Name="MyPath" StrokeThickness="2" Data="{Binding Path=Points, Converter={StaticResource ResourceKey=PointsToPathConverter}}">

在评论后添加:

我可不想和价值转换器混在一起。我在网上找到了我用过的转换器。我能修改一下吗?

代码语言:javascript
复制
[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
}
EN

回答 2

Stack Overflow用户

发布于 2013-10-30 11:31:55

你快到了..。您需要一个返回PathFigure而不是点的转换器。

如果您修改转换器,代码应该可以工作。

希望能帮上忙。

票数 0
EN

Stack Overflow用户

发布于 2013-10-30 11:38:44

无需测试它:您应该能够像这样重用Path.Data绑定表达式:

代码语言:javascript
复制
<MatrixAnimationUsingPath ...
    PathGeometry="{Binding Path=Points,
                   Converter={StaticResource ResourceKey=PointsToPathConverter}}" />

但是,我不确定您是否需要显式地设置绑定源对象,因为MatrixAnimationUsingPath对象没有DataContext。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19680947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档