locked
Why Button on ListView item cannot be dragged?

    Question

  • XAML code:

            <ListView CanDragItems="false"
                      AllowDrop="True"
                      SelectionMode="None"
                      CanReorderItems="True"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center">
                <ListView.Items>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="hello world!"
                                   VerticalAlignment="Center"
                                   Margin="30, 10" />
                        <Button Content="click me!" />
                    </StackPanel>
                </ListView.Items>
            </ListView>

    I pressed on the button and holded, then tried to drag the listview item, but nothing happened. If I drag the TextBlock, everything works as expected. Seems the Button on the listvie item cannot be dragged. What's going on behind this? What's special with Button control? Any idea would be appreciated!



    Sunday, February 1, 2015 7:45 AM

Answers

  • I think you're confusing the issue by bringing up buttons. The behavoiur I think you're looking for is only very incidental to button-ness.

    If I understand it correctly, you want a control which handles the pointer input itself rather than letting the ListView handle the input to initiate a drag.

    To do this, handle the pointer events (PointerPressed, PointerReleased, etc.) in your control and mark the PointerRoutedEventArgs as Handled rather than letting the event bubble up to the ListView.

    --Rob

    • Marked as answer by silverbird2015 Thursday, February 12, 2015 3:49 AM
    Thursday, February 12, 2015 1:15 AM
    Owner

All replies

  • Hi Silverbird2015,

    Per my understanding, listview item can be drag and drop. Not the button control and the textblock control. When you move your pointer on the button control, you will see there is a gray border around that button. That is the ListView item. If you remove the textblock control, you will still see the border and drag it.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, February 2, 2015 6:55 AM
    Moderator
  • Yes, I know listview item can be drag&drop, my question is why I can't drag it by dragging the button control on it, while I can drag it by dragging the textblock control on it. What's special with Button control?
    Monday, February 2, 2015 10:00 AM
  • Hi Silverbird2015,

    When you try to move the button in ListView, the button will get focus and trigger events in button if you have registered. At that time, the system consider you are using button not the listview item. The TextBox control has the same behavior.

    So per my understanding, If you want to drag ListView item, you should avoid using these controls.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, February 11, 2015 2:25 AM
    Moderator
  • Hi, Herro, thanks.

    Here is the thing: I want to create a Button-like control which is derived from Control directly(instead of ButtonBase), and want it behavior the same like a Button, i.e. when it is put on a ListViewItem, dragging it will not "influent" the whole ListViewItem like normal Button or TextBox.

    How to implement this Button-like feature?



    Wednesday, February 11, 2015 5:19 AM
  • I think you're confusing the issue by bringing up buttons. The behavoiur I think you're looking for is only very incidental to button-ness.

    If I understand it correctly, you want a control which handles the pointer input itself rather than letting the ListView handle the input to initiate a drag.

    To do this, handle the pointer events (PointerPressed, PointerReleased, etc.) in your control and mark the PointerRoutedEventArgs as Handled rather than letting the event bubble up to the ListView.

    --Rob

    • Marked as answer by silverbird2015 Thursday, February 12, 2015 3:49 AM
    Thursday, February 12, 2015 1:15 AM
    Owner
  • Thanks, Rob. That's exactly what I wanted!
    Thursday, February 12, 2015 3:49 AM