Notice how the cursor changes to indicate a copy.ĭrag and drop a Circle onto the TextBox. While dragging a Circle over the TextBox, press the Ctrl key. When dragging over the TextBox, the cursor changes to indicate a move. Press F5 to build and run the application.Ĭlick one of the Circle controls and drag it over the panels, the other Circle, and the TextBox. You pass the following three parameters to the DoDragDrop method:ĭragSource – A reference to this control.ĭata – The DataObject created in the previous code.ĪllowedEffects – The allowed drag-and-drop operations, which are Copy or Move. In this case, the Circle control packages three data items a string representation of its Fill color, a double representation of its height, and a copy of itself.Ĭalls the static DragDrop.DoDragDrop method to initiate the drag-and-drop operation. Packages the Circle data into a DataObject. This OnMouseMove override performs the following tasks:Ĭhecks whether the left mouse button is pressed while the mouse is moving. If e.LeftButton = MouseButtonState.Pressed Thenĭata.SetData(DataFormats.StringFormat, ())ĭragDrop.DoDragDrop(Me, data, DragDropEffects.Copy Or DragDropEffects.Move) Protected Overrides Sub OnMouseMove(ByVal e As ) If (e.LeftButton = MouseButtonState.Pressed)ĭata.SetData(DataFormats.StringFormat, ()) ĭragDrop.DoDragDrop(this, data, DragDropEffects.Copy | DragDropEffects.Move) protected override void OnMouseMove(MouseEventArgs e) To initiate a drag-and-drop operationĪdd the following OnMouseMove override to provide class handling for the MouseMove event. In this case, the Circle control will package three data items a string representation of its Fill color, a double representation of its height, and a copy of itself. If a drag is started (a mouse button is pressed and the mouse is moved), you will package the data to be transferred into a DataObject. In this section, you will override the OnMouseMove method and initiate the drag-and-drop operation. Implement Drag Source Events in the User Control The full XAML for the panel looks like the following. In the first StackPanel, add the following XAML to create two instances of the Circle user control in the first panel. xmlns:local="clr-namespace:DragDropExample" = c.circleUI.Height Īdd the following XAML to the opening Window tag to create an XML namespace reference to the current application. In the simplified Circle user control, you will only copy the Fill and the size of the of the user control. In order to allow the user control to be copied, you add a copy constructor method in the code-behind file. In Visual Basic, add the following code to create both a parameterless constructor and a copy constructor. In C#, add the following code after the parameterless constructor to create a copy constructor. This file will contain the user interface elements of the user control.Īdd the following markup to the root Grid to create a simple user control that has a blue circle as its UI. In the Add New Item dialog box, change the name to Circle.xaml, and click Add.Ĭircle.xaml and its code-behind is added to the project. On the Project menu, select Add User Control. In this section, you will add a new user control to the project. This markup creates the user interface for the test application. For more information, see Walkthrough: My first WPF desktop application.Īdd the following markup between the opening and closing Grid tags. In this section, you will create the application infrastructure, which includes a main page with two panels and a TextBox.Ĭreate a new WPF Application project in Visual Basic or Visual C# named DragDropExample. You need Visual Studio to complete this walkthrough. This walkthrough illustrates the following tasks:Įnable the user control to be a drag source.Įnable the user control to be a drop target.Įnable a panel to receive data dropped from the user control. You will write code that enables the panels to process dropped Circle data, which will enable you to move or copy Circles from the Children collection of one panel to the other. You will also create a small application that contains two panel controls and a TextBox to test the drag-and-drop functionality. If you drag from a Circle control to a TextBox, the string representation of the Fill color is copied to the TextBox. For example, if you drag from one Circle control to another, the Fill color data is copied from the source Circle to the target. You will implement functionality on the control to enable data transfer through drag-and-drop. In this walkthrough, you will create a custom WPF UserControl that represents a circle shape. This walkthrough demonstrates how to create a custom user control that can participate in drag-and-drop data transfer in Windows Presentation Foundation (WPF).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |