<Grid> <Path Data="..." Stroke="Gray" /> <Path Data="..." Stroke="Black"> <Path.Clip> <RectangleGeometry Rect="0,0,100,50" /> </Path.Clip> </Path> </Grid>
<Grid> <Path Data="..." Stroke="Gray" StrokeThickness="1" StrokeDashCap="Flat" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat"> </Path> <Path Data="..." StrokeThickness="2" StrokeDashCap="Flat" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Stroke="Black" StrokeDashArray="9 3"> </Path> </Grid>
public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register( "Progress", typeof(double), typeof(ProgressPathControl), new PropertyMetadata(0.0, OnProgressValueChanged)); public double Progress { get { return (double)GetValue(ProgressProperty); } set { SetValue(ProgressProperty, Math.Min(1.0, Math.Max(0.0, value))); } } private static void OnProgressValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != e.OldValue) { ((ProgressPathControl)d).OnProgressValueChanged((double)e.OldValue, (double)e.NewValue); } }
private double GetGeometryLength(Geometry geometry) { double result = 0; var pathGeometry = geometry as PathGeometry; if (pathGeometry != null) { foreach (var figure in pathGeometry.Figures) { var currentPoint = figure.StartPoint; foreach (var segment in figure.Segments) { var bezier = segment as BezierSegment; var line = segment as LineSegment; if (bezier != null) { result += GetBezierLength(currentPoint, bezier.Point1, bezier.Point2, bezier.Point3); currentPoint = bezier.Point3; } else if (line != null) { result += GetLineLength(currentPoint, line.Point); currentPoint = line.Point; } } } } return result; } private double GetBezierLength(Point p0, Point p1, Point p2, Point p3) { double result = 0; Point lastPoint = p0; for (double t = 0.001; t <= 1; t += 0.001) { Point currentPoint; // // https://ru.wikipedia.org/wiki/_ currentPoint.X = Math.Pow(1 - t, 3) * p0.X + 3 * t * Math.Pow(1 - t, 2) * p1.X + 3 * t * t * (1 - t) * p2.X + Math.Pow(t, 3) * p3.X; currentPoint.Y = Math.Pow(1 - t, 3) * p0.Y + 3 * t * Math.Pow(1 - t, 2) * p1.Y + 3 * t * t * (1 - t) * p2.Y + Math.Pow(t, 3) * p3.Y; double dx = currentPoint.X - lastPoint.X; double dy = currentPoint.Y - lastPoint.Y; result += Math.Sqrt(dx * dx + dy * dy); lastPoint = currentPoint; } return result; } private double GetLineLength(Point p0, Point p1) { double dx = p0.X - p1.X; double dy = p0.Y - p1.Y; return Math.Sqrt(dx * dx + dy * dy); }
double strokeDashLength = progressPathLength / progressPath.StrokeThickness * Progress; double strokeDashOffset = progressPathLength / progressPath.StrokeThickness; progressPath.StrokeDashArray = new DoubleCollection { strokeDashLength, strokeDashOffset };
public class ProgressPathControl : Control { #region ProgressProperty public static readonly DependencyProperty ProgressProperty = DependencyProperty.Register( "Progress", typeof(double), typeof(ProgressPathControl), new PropertyMetadata(0.0, OnProgressValueChanged)); public double Progress { get { return (double)GetValue(ProgressProperty); } set { SetValue(ProgressProperty, Math.Min(1.0, Math.Max(0.0, value))); } } private static void OnProgressValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != e.OldValue) { ((ProgressPathControl)d).OnProgressValueChanged(); } } #endregion public ProgressPathControl() { DefaultStyleKey = typeof(ProgressPathControl); } protected override void OnApplyTemplate() { base.OnApplyTemplate(); progressPath = (Path)GetTemplateChild("ProgressPath"); progressPathLength = GetGeometryLength(progressPath.Data); OnProgressValueChanged(); } private double GetGeometryLength(Geometry geometry) { double result = 0; var pathGeometry = geometry as PathGeometry; if (pathGeometry != null) { foreach (var figure in pathGeometry.Figures) { var currentPoint = figure.StartPoint; foreach (var segment in figure.Segments) { var bezier = segment as BezierSegment; var line = segment as LineSegment; if (bezier != null) { result += GetBezierLength(currentPoint, bezier.Point1, bezier.Point2, bezier.Point3); currentPoint = bezier.Point3; } else if (line != null) { result += GetLineLength(currentPoint, line.Point); currentPoint = line.Point; } } } } return result; } private double GetBezierLength(Point p0, Point p1, Point p2, Point p3) { double result = 0; Point lastPoint = p0; for (double t = 0.001; t <= 1; t += 0.001) { Point currentPoint; // // https://ru.wikipedia.org/wiki/_ currentPoint.X = Math.Pow(1 - t, 3) * p0.X + 3 * t * Math.Pow(1 - t, 2) * p1.X + 3 * t * t * (1 - t) * p2.X + Math.Pow(t, 3) * p3.X; currentPoint.Y = Math.Pow(1 - t, 3) * p0.Y + 3 * t * Math.Pow(1 - t, 2) * p1.Y + 3 * t * t * (1 - t) * p2.Y + Math.Pow(t, 3) * p3.Y; double dx = currentPoint.X - lastPoint.X; double dy = currentPoint.Y - lastPoint.Y; result += Math.Sqrt(dx * dx + dy * dy); lastPoint = currentPoint; } return result; } private double GetLineLength(Point p0, Point p1) { double dx = p0.X - p1.X; double dy = p0.Y - p1.Y; return Math.Sqrt(dx * dx + dy * dy); } private void OnProgressValueChanged() { if (progressPath != null) { double strokeDashLength = progressPathLength / progressPath.StrokeThickness * Progress; double strokeDashOffset = progressPathLength / progressPath.StrokeThickness; progressPath.StrokeDashArray = new DoubleCollection { strokeDashLength, strokeDashOffset }; } } private double progressPathLength; private Path progressPath; }
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="using:DoubleGis.Controls"> <Style TargetType="controls:ProgressPathControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="controls:ProgressPathControl"> <Grid> <Canvas Height="50" Width="323"> <Path Data="M10.7,45.6C10,47.5,8.1,48.9,6,48.9c-2.8,0-5-2.2-5-5s2.2-5,5-5s5,2.2,5,5l0,0h30.3c0.1,0,0.2-0.1,0.2-0.2v-4c0-1.4,0.2-2.3,0.7-2.8c0.3-0.3,0.7-0.5,1.3-0.5H49c0.1,0,0.2-0.1,0.2-0.2v-3.6c0-1.4,0.2-2.3,0.7-2.8c0.3-0.3,0.8-0.5,1.3-0.5h3.5c0.1,0,0.2-0.1,0.2-0.2V14.7c0-2.1,0.4-3.1,1.4-3.3c0.1,0,0.3,0,0.5,0l0.2,0c3.4-0.5,3.6-4,3.6-9.6V0.6H63l0,1.1c0,5.4,0.2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0.2,0.2l0,3.6c0.1,2.2,0.6,4.3,2.9,4.3H312l0,0.1c0,2.8,2.2,5,5,5c2.8,0,5-2.2,5-5s-2.2-5-5-5c-2.1,0-4,1.4-4.7,3.2" Height="50" StrokeThickness="1" StrokeDashCap="Flat" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Stroke="{TemplateBinding Background}" Canvas.Left="0" Stretch="None" Canvas.Top="0" Width="323" /> <Path Data="M10.7,45.6C10,47.5,8.1,48.9,6,48.9c-2.8,0-5-2.2-5-5s2.2-5,5-5s5,2.2,5,5l0,0h30.3c0.1,0,0.2-0.1,0.2-0.2v-4c0-1.4,0.2-2.3,0.7-2.8c0.3-0.3,0.7-0.5,1.3-0.5H49c0.1,0,0.2-0.1,0.2-0.2v-3.6c0-1.4,0.2-2.3,0.7-2.8c0.3-0.3,0.8-0.5,1.3-0.5h3.5c0.1,0,0.2-0.1,0.2-0.2V14.7c0-2.1,0.4-3.1,1.4-3.3c0.1,0,0.3,0,0.5,0l0.2,0c3.4-0.5,3.6-4,3.6-9.6V0.6H63l0,1.1c0,5.4,0.2,9,3.8,9.6c0,0,0.1,0,0.1,0c0.1,0,0.2,0,0.3,0l0.1,0c1.5,0.4,1.5,2.7,1.5,3.4V29c0,0.1,0.1,0.2,0.3,0.2h3.2c0.6,0,1.1,0.2,1.5,0.6c0.5,0.6,0.8,1.5,0.8,2.7l0,10.6l0,0.4c0,0.1,0.1,0.2,0.2,0.2h16.6c2.4,0,2.8-2.1,3-4.3l0-3.6c0-0.1,0-0.1-0.1-0.2c0,0-0.1-0.1-0.2-0.1l-0.8,0c-3.6,0-6.1-2.3-6.1-5.6c0-1.8,0.9-3.7,2.3-4.7c0.1-0.1,0.1-0.1,0.1-0.2c-0.1-0.5-0.1-0.9-0.1-1.4c0-1.8,0.9-3.5,2.5-4.6c0.1,0,0.1-0.1,0.1-0.2c0-0.1,0-0.3,0-0.4c0-2,1.7-3.5,3.9-3.5c2.3,0,4.2,1.6,4.2,3.5c0,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3,0.2-2.2,0.7-2.7c0.3-0.3,0.7-0.4,1.2-0.4h2.5c0.5,0,0.9,0.1,1.2,0.4c0.5,0.5,0.7,1.3,0.7,2.7v2.1c0,0.1,0.1,0.2,0.2,0.2h11.2l0.1,0c2.3-0.1,2.7-2.2,2.8-4.3l0-3.6c0-0.1-0.1-0.2-0.2-0.2c0,0-0.8,0-0.8,0c-3.6,0-6.1-2.3-6.1-5.6c0-1.8,0.9-3.7,2.3-4.7c0.1-0.1,0.1-0.1,0.1-0.2c-0.1-0.5-0.1-0.9-0.1-1.4c0-1.8,0.9-3.5,2.5-4.6c0.1,0,0.1-0.1,0.1-0.2c0-0.1,0-0.3,0-0.4c0-2,1.7-3.5,3.9-3.5c2.3,0,4.2,1.6,4.2,3.5c0,0.2,0,0.3,0,0.4c0,0.1,0,0.2,0.1,0.2c1.6,1.1,2.5,2.8,2.5,4.6c0,0.5,0,0.9-0.1,1.4c0,0.1,0,0.2,0.1,0.2c1.5,1.1,2.4,2.8,2.4,4.5c0,3.4-2.6,5.9-6.1,5.9l-1.2,0c-0.1,0-0.2,0.1-0.2,0.2l0,3.6c0.1,2.2,0.6,4.3,2.9,4.3H312l0,0.1c0,2.8,2.2,5,5,5c2.8,0,5-2.2,5-5s-2.2-5-5-5c-2.1,0-4,1.4-4.7,3.2" x:Name="ProgressPath" StrokeThickness="2" StrokeDashCap="Flat" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Stroke="{TemplateBinding Foreground}" Height="50" Canvas.Left="0" Stretch="None" Canvas.Top="0" Width="323" /> </Canvas> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
Source: https://habr.com/ru/post/275489/
All Articles