Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

WPF: Drag Drop Expamle

12.01.2019 (👁861)

WPF: Drag Drop Beispiel

 

The following C # Code example shows the simple creation of a drag and drop function in WPF

In this example, a rectangular planning field (WPF button) is drawn into a planning calendar (WPF Border).

With a WPF drag-and-drop operation, any data can be transmitted at runtime.

When you create a drag process, all freely definable information is placed in a DragObject when you start it.

This freely defined information can then be read out by the drag object at the target as in an array.

 

 

Programming Drag _ Drop:

 

Drag-Element

The WPF button element to be pulled requires one. PreviewMouseLeftButtonDown Event

This can be added as an event in Xaml or as here at runtime as here to an event method Start _ Drag _ Event.

 

btnMitarbeiter_Schicht.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Start_Drag_Event);

 

 

Drag Target

The element that is on eIn drag process should rule then needs the property. AllowDrop = true and the. Drop event

 

cellBorder.AllowDrop = true;

cellBorder.Drop += new DragEventHandler(Target_Border_Drop);

 

 

At the start of the Drag-Operation is then created a DragObject and this object is then loaded with all sorts of data

 

    //< set_dragData >

    //*set informations to the dragged object

    DataObject dataObj = new DataObject();

    dataObj.SetData("Name", sName);

    dataObj.SetData("Farbe", sColor);

    dataObj.SetData("IDMitarbeiter", IDMitarbeiter);

    //</ set_dragData >

 

    //< start_drag >

    //*shows drag moving icon until drops

    DragDrop.DoDragDrop(button, dataObj, DragDropEffects.Move);

    //</ start_drag >

 

 

Drop-Event:

At the drop event, the DragObject is read out directly

 

//< values_from_dragger >

    String sColor = (String)e.Data.GetData("Farbe");

    Color color = (ColorColorConverter.ConvertFromString(sColor);

    String sName = (String)e.Data.GetData("Name");

    int IDMitarbeiter = (int) e.Data.GetData("IDMitarbeiter");

    //</ values_from_dragger >

 

 

 

 

C# Code Drag and Drop

 

#region Drag_Drop

//=============< region: Drag_Drop >=============

private void Start_Drag_Event(object sender, MouseButtonEventArgs e)

{

    //---------------------< Start_Drag_Event() >---------------------

    //< init >

    //*get values from drag_button_start

    Button button = (Button)sender;

    string sName = (string) button.Content;

    string sColor = button.Background.ToString();

    string sControlname = button.Name;

    int IDMitarbeiter = clsStrings.get_Key_Value_from_String_as_Integer(sControlname, "IDMitarbeiter");

    //</ init >

 

    //< set_dragData >

    //*set informations to the dragged object

    DataObject dataObj = new DataObject();

    dataObj.SetData("Name", sName);

    dataObj.SetData("Farbe", sColor);

    dataObj.SetData("IDMitarbeiter", IDMitarbeiter);

    //</ set_dragData >

 

    //< start_drag >

    //*shows drag moving icon until drops

    DragDrop.DoDragDrop(button, dataObj, DragDropEffects.Move);

    //</ start_drag >

 

    //---------------------</ Start_Drag_Event() >---------------------

}

 

private void Target_Border_Drop(object sender, DragEventArgs e)

{

    //---------------------< Target_Border_Drop() >---------------------

    //*event when drop on target control 

    Border target_Control = (Border)sender;

 

    //< values_from_dragger >

    String sColor = (String)e.Data.GetData("Farbe");

    Color color = (ColorColorConverter.ConvertFromString(sColor);

    String sName = (String)e.Data.GetData("Name");

    int IDMitarbeiter = (int) e.Data.GetData("IDMitarbeiter");

    //</ values_from_dragger >

 

 

 

    //--< save_as_DatePlan_Entry >--

    string sPanelName = target_Control.Name;

    int IDMaschine = clsStrings.get_Key_Value_from_String_as_Integer(sPanelName , "IDMaschine");

    int IDSchicht = clsStrings.get_Key_Value_from_String_as_Integer(sPanelName, "IDSchicht");

    int PosNrBesetzung = clsStrings.get_Key_Value_from_String_as_Integer(sPanelName, "PosNrBesetzung");

    DateTime DatePlan = clsStrings.get_Key_Value_from_String_as_Date(sPanelName, "DatePlan");

 

    //< save >

    //eintrag in die Datenbank

    clsSchichtplan.erstelle_Schichtplan_Eintrag(IDMaschine, IDSchicht, IDMitarbeiter, DatePlan, PosNrBesetzung);

    //</ save >

    //--</ save_as_DatePlan_Entry >--

 

 

    //< show_Button >

    //*insert child_button with name

    Button child_Button = new Button();

    child_Button.Background = new SolidColorBrush(color);

    child_Button.Content = sName;

    child_Button.Name = "";

    target_Control.Child = child_Button;

    //</ show_Button >

    //---------------------</ Target_Border_Drop() >---------------------

}

 

//=============</ region: Drag_Drop >=============

#endregion /Drag_Drop

 

 

 

 

On the completeness of the example:

 

Drag-those

Here is the code to create a button during the runtime.

 

//--< cellButton_Mitarbeiter_erstellen >--

 

Button btnMitarbeiter_Schicht = new Button();

btnMitarbeiter_Schicht.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(sColor_SchichtGruppe));

btnMitarbeiter_Schicht.Content = mitarbeiter.Nachname;

btnMitarbeiter_Schicht.Name = "button_IDMitarbeiter_" + mitarbeiter.IDMitarbeiter;

btnMitarbeiter_Schicht.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Start_Drag_Event);

 

Set_Position_of_Element_in_a_Grid(btnMitarbeiter_Schicht, rowNr, iColNr);

Grid_Mitarbeiter.Children.Add(btnMitarbeiter_Schicht);

//--</ cellButton_Mitarbeiter_erstellen >--

 

 

 

Drag receiver

 

//-< panel >-

Border cellBorder = new Border();

Color color = (Color)ColorConverter.ConvertFromString(_sColor_Cell);

cellBorder.Background = new SolidColorBrush(color);

cellBorder.Margin = new Thickness(1);

cellBorder.MouseUp += Panel_MouseUp;

 

cellBorder.AllowDrop = true;

cellBorder.Drop += new DragEventHandler(Target_Border_Drop);

//--< Cell_name >--

string sCellBorder_Name = "cellBorder_IDMaschine_" + maschine.IDMaschine + "_IDSchicht_" + IDSchicht  + "_PosNrMitarbeiter_" + iMitarbeiter + "_DatePlan_" + sDatePlan;

cellBorder.Name = sCellBorder_Name;  

 

//--</ Cell_name >--

Set_Position_of_Element_in_a_Grid(cellBorder, rowNr, iColNr);

Grid_Plan.Children.Add(cellBorder);

//-</ panel >-