\

In the cone of light, all is fate

【WPF】记录一些控件的实现

1.流动路径

可以通过Path来定义路线,虚线从起点流向终点
<UserControl.Resources>
    <Storyboard x:Key="OnLoaded1" x:Name="_Storyboard" >
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeDashOffset)" Storyboard.TargetName="path" RepeatBehavior="Forever">
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="-20"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>

 <Canvas>
    <Path x:Name="path" Stroke="xxx" 
      StrokeThickness="xxx" StrokeDashArray="3,2">
        <Path.Data>
            <PathGeometry>
                <PathFigure StartPoint="{Binding PathStartPoint, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXXX}}}">
                    <PolyLineSegment x:Name="_Path" Points="{Binding PathPoints, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXXX}}}"></PolyLineSegment>
                </PathFigure>
            </PathGeometry>
        </Path.Data>
    </Path>
</Canvas>
public XXXX()
{
    InitializeComponent();

    var board = (Storyboard)this.FindResource("OnLoaded1");
    board.Begin(this, true);
}

/// <summary>
/// 除起点外的路径
/// </summary>
public PointCollection PathPoints
{
    get { return (PointCollection)GetValue(PathPointsProperty); }
    set { SetValue(PathPointsProperty, value); }
}
public static readonly DependencyProperty PathPointsProperty =
    DependencyProperty.Register("PathPoints", typeof(PointCollection), typeof(XXXX));

/// <summary>
/// 暂时或开始
/// </summary>
public bool IsPauseStoryboard
{
    get { return (bool)GetValue(IsPauseStoryboardProperty); }
    set { SetValue(IsPauseStoryboardProperty, value); }
}
public static readonly DependencyProperty IsPauseStoryboardProperty =
    DependencyProperty.Register("IsPauseStoryboard", typeof(bool), typeof(XXXX), new PropertyMetadata(false, (d, e) => (d as XXXX)?.PauseOrBegin()));

/// <summary>
/// 路径起点,动画是从起点流向终点
/// </summary>
public Point PathStartPoint
{
    get { return (Point)GetValue(PathStartPointProperty); }
    set { SetValue(PathStartPointProperty, value); }
}
public static readonly DependencyProperty PathStartPointProperty =
    DependencyProperty.Register("PathStartPoint", typeof(Point), typeof(XXXX), new PropertyMetadata(new Point(0, 0)));

/// <summary>
/// 暂时或开始
/// </summary>
private void PauseOrBegin()
{
    var board = (Storyboard)this.FindResource("OnLoaded1");
    if (IsPauseStoryboard)
    {
        board.Pause(this);
    }
    else
    {
        board.Resume(this);
    }
}
posted @ 2023-02-10 13:10  Ymrt  阅读(38)  评论(0编辑  收藏  举报