Example of Wpf drag/drop sliding effect in asp.net

Source: Internet
Author: User

In fact, it is very easy to support drag and drop in wpf. You can use drag events or custom mouse events.

Today, we will share a drag-and-drop slide effect with mouse clicks and up events.

First, define a ScrollViewer in xaml.

The code is as follows: Copy code
<Window x: Class = "Wpf drag/drop sliding effect. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "400" Width = "680">
<Grid>
<ScrollViewer Name = "viewer" Grid. Column = "0" Grid. Row = "1" Padding = "0, 0, 0" VerticalScrollBarVisibility = "Auto">
<Canvas Name = "canvas" Margin = "0, 0, 0" PreviewMouseDown = "canvas_PreviewMouseDown" PreviewMouseMove = "canvas_PreviewMouseMove" PreviewMouseUp = "canvas_PreviewMouseUp">
<Grid Name = "NGEntry">
 
</Grid>
</Canvas>
</ScrollViewer>
</Grid>
</Window>

Simulate button data in the background:

The code is as follows: Copy code
/// <Summary>
/// Create a button set
/// </Summary>
/// <Param name = "grid"> </param>
Public void CreateNgEtry (ref Grid grid)
 {
Grid. Children. Clear ();
Grid. RowDefinitions. Clear ();
Grid. ColumnDefinitions. Clear ();
For (int I = 0; I <6; I ++)
     {
     }
For (int I = 0; I <18 + 1; I ++)
     {
Grid. RowDefinitions. Add (new RowDefinition ());
     }
For (int I = 0; I <18; I ++)
     {
Grid. ColumnDefinitions. Add (new ColumnDefinition ());
For (int j = 0; j <6; j ++)
  {
Button but = new Button ();
But. Height = 80;
But. Width = 110;
But. FontSize = 16;
Grid. Children. Add ();
System. Windows. Controls. Grid. SetColumn (but, j );
System. Windows. Controls. Grid. SetRow (but, I );
But. Content = "Test" + I. ToString ();
  }
     }
 }

Handle the event when you press the mouse, and remember the current position:

The code is as follows: Copy code
Private void canvas_PreviewMouseDown (object sender, MouseButtonEventArgs e)
 {
TargetElement = Mouse. DirectlyOver as UIElement;
If (targetElement! = Null)
     {
TargetPoint = e. GetPosition (NGEntry );
     }
 }

When dragging the mouse:

The code is as follows: Copy code

Private void canvas_PreviewMouseMove (object sender, MouseEventArgs e)
 {
If (e. LeftButton = MouseButtonState. Pressed & targetElement! = Null)
     {
Var pCanvas = e. GetPosition (canvas );
Double gridtop = Convert. ToDouble (NGEntry. GetValue (Canvas. TopProperty ));
NGEntry. SetValue (Canvas. TopProperty, pCanvas. Y-targetPoint. Y );
   
     }
 }

When you move the mouse down the event:

The code is as follows: Copy code

Private void canvas_PreviewMouseUp (object sender, MouseButtonEventArgs e)
 {
Double gridtop = Convert. ToDouble (NGEntry. GetValue (Canvas. TopProperty ));
Double gridh = NGEntry. ActualHeight;
Double viewh = canvas. ActualHeight;
If (gridtop> 0)
     {
NGEntry. SetValue (Canvas. TopProperty, 0d );
     }
If (viewh + (Math. Abs (gridtop)> gridh)
     {
NGEntry. SetValue (Canvas. TopProperty, viewh-gridh );
     }
 }

Related Article

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.