"C#/WPF" uses thumb to make a draggable UI control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.