Original: "C#/WPF" uses thumb to make a draggable UI control
Requirements: Simple draggable pictures
Using the System.Windows.Controls.Primitives.Thumb class
Front desk:
<Canvas x:name="G"> <Thumb canvas.left="Ten" canvas.top= " canvas.zindex" ="dragdelta" = "Thumb_dragdelta"> <thumb.template> <ControlTemplate> <image Width=" max" Height= "All" Source="Your picture path" /> </ControlTemplate> </thumb.template> </Thumb></Canvas>
Background:
private void Thumb_dragdelta (object sender, System. Windows. Controls. Primitives. DragDeltaEventArgse) {UIElement thumb = E. SourceAs UIElement;Prevents the thumb control from being dragged out of the container. if (NTop <=0)//NTop =0;if (NTop >= (g. Height-Mythumb. Height))//NTop = g. Height-Mythumb. Height;if (Nleft <=0)//Nleft =0;if (Nleft >= (g. Width-Mythumb. Width))//Nleft = g. Width-Mythumb. Width;Canvas. Settop(Mythumb, NTop);Canvas. Setleft(Mythumb, Nleft);Tt. Text="Top:"+ NTop. ToString() +"\nleft:"+ Nleft. ToString();Canvas. Setleft(Thumb, Canvas. GetLeft(thumb) + E. Horizontalchange);Canvas. Settop(Thumb, Canvas. GetTop(thumb) + E. Verticalchange);}
Pit point:
- The thumb must be a subclass of a canvas, because it relies on the canvas to locate it.
- The thumb must negotiate canvas.left and canvas.top, because it relies on the vanvas to locate it, otherwise it cannot be dragged!
Important References:
- https://wpf.2000things.com/2012/12/19/715-using-the-thumb-control-to-drag-objects-on-a-canvas/
- Https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb (v=vs.110). aspx
"C#/WPF" uses thumb to make a draggable UI control