在画布上我有一个由RotateTransform通过动画旋转的椭圆。我希望添加一条线,其一端连接到椭圆上的一个点。我可以以某种方式绑定到椭圆上的一个点吗?

有帮助吗?

解决方案

您可以同时为Ellipse和线设置动画,如下所示:

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas.Resources>
        <PathGeometry x:Key="lineEndPath">
            <PathFigure StartPoint="25,50">
                <ArcSegment IsLargeArc="True" Point="100,50" Size="25,25" SweepDirection="Clockwise"/>
                <ArcSegment IsLargeArc="True" Point="25,50" Size="25,25" SweepDirection="Clockwise"/>
            </PathFigure>
        </PathGeometry>
    </Canvas.Resources>
    <Canvas.Triggers>
        <EventTrigger RoutedEvent="Canvas.Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Duration="0:0:5" From="0" RepeatBehavior="Forever" Storyboard.TargetName="rotTF" Storyboard.TargetProperty="Angle" To="360"/>
                    <PointAnimationUsingPath Duration="0:0:5" PathGeometry="{StaticResource lineEndPath}" RepeatBehavior="Forever" Storyboard.TargetName="lineEndPoint" Storyboard.TargetProperty="Point"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Canvas.Triggers>
    <Ellipse Width="75" Height="50" Canvas.Left="25" Canvas.Top="25" Stroke="Black">
        <Ellipse.RenderTransform>
            <RotateTransform x:Name="rotTF" CenterX="37.5" CenterY="25"/>
        </Ellipse.RenderTransform>
    </Ellipse>
    <Path Stroke="Black" StrokeThickness="1.0">
        <Path.Data>
            <PathGeometry>
                <PathFigure StartPoint="0,0">
                    <LineSegment x:Name="lineEndPoint"/>
                </PathFigure>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path Data="{StaticResource lineEndPath}" Stroke="Black" StrokeDashArray="2,0,0" StrokeThickness="1.0"/>
</Canvas>

我们为 LineSegment 使用 PointAnimationUsingPath ,并将路径设置为圆圈(由虚线显示)。

其他提示

我不确定问题是什么。您可以在Canvas中添加另一个正确排列的元素,并将变换应用到将旋转这两个元素的画布吗?

如果你问是否有任何方式可以说“与此对齐”在线,然后没有,你现在不能做到这一点。对于像这样的复杂布局,您可以使用KaXaml / Bland进行试验和错误,或者使用Illustrator进行布局,然后导出到XAML。

假设我理解正确,你将不得不弄清楚改变你的线的终点的数学。对不起,不要随便知道公式,但你基本上会在椭圆上找到你的点,然后在给定旋转角度的情况下找出它的位置,然后用这些信息改变你的线的终点。

为了通过Line连接边缘处的两个元素,我使用了一个边界框方法,如用一条线连接两个WPF画布元素,而不使用锚点?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top