Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So...having become the proud owner of a tumbleweed badge, I've got back on to this to try &amp; find a way around it. ;-)</p> <p>I'm not sure I like the solution so I'm still very much open to any better approaches.</p> <p>Basically, what I ended up doing is remember what ListBoxItem was last clicked on &amp; then make sure that gets added to the selected items before a drag. This also meant looking at how far the mouse moves before starting a drag - because clicking on a selected item to unselect it could sometimes result in it getting selected again if mouse bounce started a little drag operation.</p> <p>Finally, I added some hot tracking to the listbox items so, if you mouse down on a selected item it'll get unselected but you still get some feedback to indicate that it will get included in the drag operation.</p> <pre><code>private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e) { var source = (FrameworkElement)sender; var hitItem = source.InputHitTest(e.GetPosition(source)) as FrameworkElement; hitListBoxItem = hitItem.FindVisualParent&lt;ListBoxItem&gt;(); origPos = e.GetPosition(null); } private void HandleLeftButtonUp(object sender, MouseButtonEventArgs e) { hitListBoxItem = null; } private void HandleMouseMove(object sender, MouseEventArgs e) { if (ShouldStartDrag(e)) { hitListBoxItem.IsSelected = true; var sourceItems = (FrameworkElement)sender; var viewModel = (WindowViewModel)DataContext; DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move); hitListBoxItem = null; } } private bool ShouldStartDrag(MouseEventArgs e) { if (hitListBoxItem == null) return false; var curPos = e.GetPosition(null); return Math.Abs(curPos.Y-origPos.Y) &gt; SystemParameters.MinimumVerticalDragDistance || Math.Abs(curPos.X-origPos.X) &gt; SystemParameters.MinimumHorizontalDragDistance; } </code></pre> <p>XAML changes to include hot tracking...</p> <pre><code>&lt;Style TargetType="ListBoxItem"&gt; &lt;Setter Property="Margin" Value="1"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt; &lt;Grid&gt; &lt;Border Background="{TemplateBinding Background}" /&gt; &lt;Border Background="#BEFFFFFF" Margin="1"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt;&lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Border Margin="1" Grid.Row="0" Background="#57FFFFFF" /&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;ContentPresenter Margin="8,5" /&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="Background" Value="PowderBlue" /&gt; &lt;/Trigger&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="IsMouseOver" Value="True" /&gt; &lt;Condition Property="IsSelected" Value="False"/&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="Background" Value="#5FB0E0E6" /&gt; &lt;/MultiTrigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre>
 

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