Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to view drag & drop element in WPF?
    primarykey
    data
    text
    <p>I have a <code>ListBox</code> and a <code>DockPanel</code>. List box contains items that are supposed to be dragged onto the dock panel. I've implemented that by following this <a href="http://www.wpftutorial.net/DragAndDrop.html" rel="nofollow">link</a>. </p> <p>There are a couple of things I do not understand though:</p> <ol> <li>While dragging, all I see is a cursor. I'd like to literary see the list item I am dragging to move around with my cursor. How do I do that?</li> <li>Is the <code>DragDropEffect</code> property only for the different cursor design or it has a higher purpose? :)</li> <li>How do I make list item disappear from the <code>ListBox</code> once it is dropped onto the <code>DockPanel</code>?</li> <li>I'd like to enforce some animation on the items that I drag, like glow once it is dropped. Which trigger/setter should I use for that?</li> </ol> <p>Here's my code for basic dragging and dropping: </p> <h3>Code-behind for the ListBox part</h3> <pre><code>private Point startPosition; private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPosition = e.GetPosition(null); } private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e) { Point currentPosition; Vector offset; ListBox listBox; ListBoxItem item; Match match; DataObject dragData; currentPosition = e.GetPosition(null); offset = startPosition - currentPosition; if (e.LeftButton == MouseButtonState.Pressed &amp;&amp; (Math.Abs(offset.X) &gt; SystemParameters.MinimumHorizontalDragDistance || Math.Abs(offset.Y) &gt; SystemParameters.MinimumVerticalDragDistance)) { // Get the data binded to ListBoxItem object, which is "match" listBox = sender as ListBox; item = FindAnchestor&lt;ListBoxItem&gt;((DependencyObject)e.OriginalSource); match = (Match)listBox.ItemContainerGenerator.ItemFromContainer(item); dragData = new DataObject("match", match); DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move); } } </code></pre> <h3>Code-behind for the DockPanel part</h3> <pre><code>private void DockPanel_DragEnter(object sender, DragEventArgs e) { if (!e.Data.GetDataPresent("match") || sender == e.Source) { e.Effects = DragDropEffects.None; } } private void DockPanel_Drop(object sender, DragEventArgs e) { Match match; DockPanel matchSlot; ContentPresenter contentPresenter; Binding binding; if (e.Data.GetDataPresent("match")) { match = e.Data.GetData("match") as Match; matchSlot = sender as DockPanel; contentPresenter = new ContentPresenter(); contentPresenter.ContentTemplate = this.FindResource("MatchTemplate") as DataTemplate; binding = new Binding(); binding.Source = match; contentPresenter.SetBinding(ContentPresenter.ContentProperty, binding); matchSlot.Children.Clear(); matchSlot.Children.Add(contentPresenter); } } </code></pre> <p>Thanks for all the help. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload