开发者

WPF : How do I make a editable path

开发者 https://www.devze.com 2023-02-15 18:05 出处:网络
I was wondering if someone could guide me on this problem : I need to create a path by clicking several points on a canvas, and these points would be added to the path geometry. After finishing the p

I was wondering if someone could guide me on this problem :

I need to create a path by clicking several points on a canvas, and these points would be added to the path geometry. After finishing the path, the user can "Slide" or Move the Control Vertices (anchors) of the path to adjust the shape of the path.

I have figured out how to draw a "lasso" style path, but how do i allow the user to select an开发者_高级运维d move a single point in the path ???


You will have to develop a data structure to store the point data so that it can be easily queried and manipulated. The Path object may be sufficient for this itself, but consider wrapping it in another object to present a more domain specific interface.

You will have to detect mouse events in the Canvas and hit test for all the vertices in the Path.

A hit test is a function that gives you a reference to a single point in the path nearest to the coordinate of the mouse or null if the click was too far from any of the points to be considered a "hit". Your hit test function becomes a low level construct from which you can build the more interesting editing operations.

For instance, you can store a bool for each point in the path indicating whether the point is selected. When you drag the mouse with the button down, you can drag all the selected points by offsetting their data in the data structure mentioned earlier.


I would try the following:

  1. In C#, have an ObservableCollection<Point>, or perhaps a PointCollection.
  2. From that collection, via data-binding, I would draw a Path and get its geometry from the collection, somehow;
  3. Above the path layer itself, I would add an ItemsContainer of some sort, setting the ItemsTemplate to be a System.Windows.Controls.Primitives.Thumb (Control that handles dragging), with a ControlTemplate with shape of Ellipse and DataTrigger changing its appearence based on being sellected or not. ItemsSource will be bound to the collection, too.

Doing that, you'll hit-test the path, for example, to highlight its points, which will switch the ItemsContainer's visibility (and thus hit-testing) on.

With these, you can use regular events like "Drag", "MouseMove", etc. handling hit-test directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消