Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:drag_and_drop [2010/11/15 14:35]
smayr
swdev:dotnet:drag_and_drop [2011/04/06 08:44] (current)
smayr
Line 2: Line 2:
  
 == Source Control (Drag Origin) ==  == Source Control (Drag Origin) == 
 +Sample ListBox (XAML) where Drag originates:
 +<code xml>
 +<ListBox Name="lstEnvironments" 
 +               SelectionChanged="lstEnvironments_SelectionChanged" 
 +               PreviewMouseLeftButtonDown="lstEnvironments_PreviewMouseLeftButtonDown" 
 +               MouseMove="lstEnvironments_MouseMove">
 +          <ListBoxItem>Normal</ListBoxItem>
 +          <ListBoxItem>Restaurant/Party</ListBoxItem>
 +          <ListBoxItem>Telephone</ListBoxItem>
 +          <ListBoxItem>Music</ListBoxItem>
 +</ListBox>
 +</code>
 +
 +Code behind:
 +<code csharp>
 +private void lstEnvironments_SelectionChanged(object sender, SelectionChangedEventArgs e)
 +{
 +    string myDragData = ((ListBoxItem)lstEnvironments.SelectedItem).Content.ToString();
 +    lblSelectedEnvironment.Text = string.Format("{0}", myDragData);  // selected data
 +}
 +
 +private void lstEnvironments_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 +{
 +    // Store the mouse position
 +    startPoint = e.GetPosition(null);
 +}
 +
 +private void lstEnvironments_MouseMove(object sender, MouseEventArgs e)
 +{
 +    // Get the current mouse position
 +    Point mousePos = e.GetPosition(null);
 +    Vector diff = startPoint - mousePos;
 +
 +    if (e.LeftButton == MouseButtonState.Pressed &&
 +        Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
 +        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
 +    {
 +        // Get the dragged ListBoxItem
 +        ListBox lstBox = sender as ListBox;
 +        ListBoxItem lstBoxItem = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);
 +
 +        // Find the data behind the ListBoxItem
 +        //string str = (string)lstBox.ItemContainerGenerator.ItemFromContainer(lstBoxItem);
 +        string str = ((ListBoxItem)lstBox.SelectedItem).Content.ToString();
 +
 +        // Initialize the drag & drop operation
 +        DataObject dragData = new DataObject(typeof(string), str);
 +        DragDrop.DoDragDrop(lstBoxItem, dragData, DragDropEffects.Move);
 +    } 
 +}
 +
 +// Helper to search up the VisualTree
 +private static T FindAnchestor<T>(DependencyObject current)
 +    where T : DependencyObject
 +{
 +    do
 +    {
 +        if (current is T)
 +        {
 +            return (T)current;
 +        }
 +        current = VisualTreeHelper.GetParent(current);
 +    }
 +    while (current != null);
 +    return null;
 +}
 +</code>
  
 == Target Control (Drop Target) == == Target Control (Drop Target) ==
 +
 +Target controls where Drop is performed.  In this case, a ''ListBox'', ''TextBlock'', and ''TextBox''.
 +<code xml>
 +<TextBlock FontWeight="Bold" Margin="0,10,0,0">Dragged Items</TextBlock>
 +<TextBlock>ListBox:</TextBlock>
 +<ListBox Name="lstAppliedEnvironment" AllowDrop="True" Height="100"  SelectionMode="Single"
 +         DragEnter="lstAppliedEnvironment_DragEnter"
 +         Drop="lstAppliedEnvironment_Drop">
 +</ListBox>
 +<StackPanel Orientation="Horizontal">
 +    <TextBlock>TextBlock:</TextBlock>
 +    <TextBlock Name="lblAppliedEnvironment" Margin="5,0,0,20" AllowDrop="True"
 +           DragEnter="lblAppliedEnvironment_DragEnter" Drop="lblApliedEnvironment_Drop">Value</TextBlock>
 +</StackPanel>
 +<TextBox Name="txtAppliedEnvironment" Height="30" AllowDrop="True" 
 +         DragEnter="txtSelectedEnvironment_DragEnter" 
 +         Drop="txtAppliedEnvironment_Drop"></TextBox>
 +</code>
 +
 +Code behind:
 +<code csharp>
 +private void lstAppliedEnvironment_DragEnter(object sender, DragEventArgs e)
 +{
 +    if (!e.Data.GetDataPresent("str") || sender == e.Source)
 +    {
 +        e.Effects = DragDropEffects.None;
 +    }
 +}
 +
 +private void lstAppliedEnvironment_Drop(object sender, DragEventArgs e)
 +{
 +    if (e.Data.GetDataPresent(typeof(string)))
 +    {
 +        string str = e.Data.GetData(typeof(string)) as string;
 +        ListBox lstBox = sender as ListBox;
 +        lstBox.Items.Add(str);
 +    }
 +}
 +
 +private void lblAppliedEnvironment_DragEnter(object sender, DragEventArgs e)
 +{
 +    if (!e.Data.GetDataPresent("str") || sender == e.Source)
 +    {
 +        e.Effects = DragDropEffects.None;
 +    }
 +}
 +
 +private void lblAppliedEnvironment_Drop(object sender, DragEventArgs e)
 +{
 +    if (e.Data.GetDataPresent(typeof(string)))
 +    {
 +        string str = e.Data.GetData(typeof(string)) as string;
 +        TextBlock lbl = sender as TextBlock;
 +        lbl.Text = str;
 +    }
 +}
 +</code>
  
 == Resources == == Resources ==
   * [[http://blog.nostatic.org/2007/12/drag-and-drop-in-wpf.html|Drag and Drop in WPF]]   * [[http://blog.nostatic.org/2007/12/drag-and-drop-in-wpf.html|Drag and Drop in WPF]]
-  * [[http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx|Drag and Drop in WPF Explained End to End]]+  * [[http://blogs.msdn.com/b/jaimer/archive/2007/07/12/drag-drop-in-wpf-explained-end-to-end.aspx|Drag and Drop in WPF Explained End to End]] (Jaime Rodríguez)
   * [[http://msdn.microsoft.com/en-us/library/ms742859.aspx|MSDN: Drag and Drop Overview]]   * [[http://msdn.microsoft.com/en-us/library/ms742859.aspx|MSDN: Drag and Drop Overview]]
   * [[http://wpftutorial.net/DragAndDrop.html|WPF Tutorial: Drag and Drop]]   * [[http://wpftutorial.net/DragAndDrop.html|WPF Tutorial: Drag and Drop]]
 +  * [[http://www.switchonthecode.com/tutorials/winforms-using-custom-cursors-with-drag-drop|WinForms using custom cursors with Drag and Drop]] 
 +  * [[http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx|Dragging Elements in Canvas]]