Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 410981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:56:30+00:00 2026-05-12T17:56:30+00:00

I’ve almost got this working apart from one little annoying thing… Because the ListBox

  • 0

I’ve almost got this working apart from one little annoying thing…

Because the ListBox selection happens on mouse down, if you start the drag with the mouse down when selecting the last item to drag it works fine, but if you select all the items to drag first and then click on the selection to start dragging it, the one you click on gets unselected and left behind after the drag.

Any thoughts on the best way to get around this?

<DockPanel LastChildFill="True">
    <ListBox ItemsSource="{Binding SourceItems}"
             SelectionMode="Multiple"
             PreviewMouseLeftButtonDown="HandleLeftButtonDown"
             PreviewMouseLeftButtonUp="HandleLeftButtonUp"
             PreviewMouseMove="HandleMouseMove"
             MultiSelectListboxDragDrop:ListBoxExtension.SelectedItemsSource="{Binding SelectedItems}"/>
    <ListBox ItemsSource="{Binding DestinationItems}"
             AllowDrop="True"
             Drop="DropOnToDestination"/>
<DockPanel>

…

public partial class Window1
{
    private bool clickedOnSourceItem;

    public Window1()
    {
        InitializeComponent();
        DataContext = new WindowViewModel();
    }

    private void DropOnToDestination(object sender, DragEventArgs e)
    {
        var viewModel = (WindowViewModel)
                            e.Data.GetData(typeof(WindowViewModel));
        viewModel.CopySelectedItems();
    }

    private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var sourceElement = (FrameworkElement)sender;
        var hitItem = sourceElement.InputHitTest(e.GetPosition(sourceElement))
                      as FrameworkElement;

        if(hitItem != null)
        {
            clickedOnSourceItem = true;
        }
    }

    private void HandleLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        clickedOnSourceItem = false;
    }

    private void HandleMouseMove(object sender, MouseEventArgs e)
    {
        if(clickedOnSourceItem)
        {
            var sourceItems = (FrameworkElement)sender;
            var viewModel = (WindowViewModel)DataContext;

            DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move);
            clickedOnSourceItem = false;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:56:31+00:00Added an answer on May 12, 2026 at 5:56 pm

    So…having become the proud owner of a tumbleweed badge, I’ve got back on to this to try & find a way around it. 😉

    I’m not sure I like the solution so I’m still very much open to any better approaches.

    Basically, what I ended up doing is remember what ListBoxItem was last clicked on & 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.

    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.

    private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var source = (FrameworkElement)sender;
        var hitItem = source.InputHitTest(e.GetPosition(source)) as FrameworkElement;
        hitListBoxItem = hitItem.FindVisualParent<ListBoxItem>();
        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) > SystemParameters.MinimumVerticalDragDistance ||
      Math.Abs(curPos.X-origPos.X) > SystemParameters.MinimumHorizontalDragDistance;
    }
    

    XAML changes to include hot tracking…

    <Style TargetType="ListBoxItem">
        <Setter Property="Margin" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid>
                      <Border Background="{TemplateBinding Background}" />
                      <Border Background="#BEFFFFFF" Margin="1">
                        <Grid>
                          <Grid.RowDefinitions>
                            <RowDefinition /><RowDefinition />
                          </Grid.RowDefinitions>
                          <Border Margin="1" Grid.Row="0" Background="#57FFFFFF" />
                        </Grid>
                      </Border>
                      <ContentPresenter Margin="8,5" />
                    </Grid>
                    <ControlTemplate.Triggers>
                      <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="PowderBlue" />
                      </Trigger>
                      <MultiTrigger>
                        <MultiTrigger.Conditions>
                          <Condition Property="IsMouseOver" Value="True" />
                          <Condition Property="IsSelected" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#5FB0E0E6" />
                      </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.