I think this is a very interesting problem, which has been proposed before. Seems it's not natively supported right now. I've tried to do it by animating an object programmatically with a timer, but couldn't.
My problem lies in that I can't determine the function value of my path for a given x value (assuming my path can be described as a function). You can also think of it as detecting the intersection points of your path and a vertical line on a given x (left) position.
This is my shot at it:
"LayoutRoot" Background="White">
"MyPath" Height="139" Width="266" Canvas.Left="19.5" Canvas.Top="128.5" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M20,211 C89,211 96,159 96,159 L101,140 L113,129 L121,133 L125,148 C125,148 125,172 125,173 C125,174 131,260 131,260 L133,267 L139,262 L143,234 L146,174 L149,157 L160,150 L167,158 L170,166 L173,178 C173,178 163,210 285,210"/>
"SomeCtrl" Height="11" Width="10" Canvas.Left="9.5" Canvas.Top="182" Fill="#FFFDFF04"/>
"btnGo" Height="23" Width="35" Canvas.Left="321" Canvas.Top="21" Content="Go" Click="btnGo_Click"/>
with code behind,
private DispatcherTimer _timer = new DispatcherTimer();
private List pathCoordinates;
private int _index;
public Page()
{
InitializeComponent();
_index = 0;
pathCoordinates = GetPathAsPointList(MyPath);
_timer.Interval = TimeSpan.FromMilliseconds(200);
_timer.Tick += new EventHandler(_timer_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
SomeCtrl.SetValue(Canvas.TopProperty, (double)pathCoordinates[_index].Y);
SomeCtrl.SetValue(Canvas.LeftProperty, (double)pathCoordinates[_index].X);
_index++;
}
private void btnGo_Click(object sender, RoutedEventArgs e)
{
_timer.Start();
}
private List GetPathAsPointList(Path p)
{
List discretePath = new List();
double left = p.Data.Bounds.Left;
double top = p.Data.Bounds.Top;
double pwidth = p.Data.Bounds.Width;
double pheight = p.Data.Bounds.Height;
p.StrokeThickness = 2;
for(int i = 0;ifor(int j = 0;jnew Point(left + i, top + j);
IEnumerable hits =
VisualTreeHelper.FindElementsInHostCoordinates(p1, LayoutRoot);
if(hits.Count()>0)
{
discretePath.Add(p1);
}
}
}
return discretePath;
}
but the discretePath.Add(p1) never gets invoked :(
(please mark as answer if this post answered your question)
Gabriel