Don't drag when selecting text with mouse

5 posts, 1 answers
  1. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 17 Aug 2012 Link to this post

    I have a RadListBox with DragDropBehavior, the RadListBox itemtemplates contain a textbox. I want the text in the textbox to be selectable using the mouse, but when I drag to select text, the item is dragged instead. Is it possible to disable drag within a control in an itemtemplate in a draggable RadListBox? I tried setting telerik:DragDropManager.AllowCapturedDrag="False" and telerik:DragDropManager.AllowDrag="False" on the textbox but that doesn't help.

    Here's my little test project:

    using System.Collections.ObjectModel;
    namespace ListBoxTest
        public partial class MainWindow
            public MainWindow()
                DataContext = this;
                MyCollection = new ObservableCollection<string> { "test text 1", "test text 2", "test text 3", "test text 4", "test text 5", "test text 6" };
            public ObservableCollection<string> MyCollection { get; private set; }

        Title="MainWindow" Height="500" Width="525">
            ItemsSource="{Binding MyCollection}">
                    <Grid Height="50" Width="100" Background="LightBlue" telerik:DragDropManager.AllowCapturedDrag="True">
                            Text="{Binding .}"
                <telerik:ListBoxDragDropBehavior />
  2. Answer
    George avatar
    1332 posts

    Posted 22 Aug 2012 Link to this post


    This behavior is expected because the RadListBoxItem container has AllowCapturedDrag set to true. In order to workaround the problem, I would suggest to remove the capture drag on TextBox_PreviewMouseLeftButtonDown handler and return it back in the TextBox_MouseLeave handler. Please, refer to the attached project and let me know if this helps.

    Kind regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 24 Aug 2012 Link to this post

    Thanks that works great!

    I created an attached behavior based on your code, and added some code to prevent dragged text from having a the same template as the listboxitems:

    public class NoDragBehavior : Behavior<FrameworkElement>
            private FrameworkElement Element
                get { return AssociatedObject; }
            public DataTemplate DragDataTemplate
                get { return (DataTemplate)GetValue(DragDataTemplateProperty); }
                set { SetValue(DragDataTemplateProperty, value); }
            public static readonly DependencyProperty DragDataTemplateProperty = DependencyProperty.Register
            protected override void OnAttached()
                Element.PreviewMouseLeftButtonDown += ElementOnPreviewMouseLeftButtonDown;
                Element.MouseLeave += ElementMouseLeave;
            protected override void OnDetaching()
                Element.PreviewMouseLeftButtonDown -= ElementOnPreviewMouseLeftButtonDown;
                Element.MouseLeave -= ElementMouseLeave;
            private static void ElementOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
                var listboxItem = (sender as FrameworkElement).ParentOfType<RadListBoxItem>();
                if (listboxItem == null) return;
                DragDropManager.SetAllowCapturedDrag(listboxItem, false);
                var listbox = listboxItem.ParentOfType<RadListBox>();
                listbox.DragVisualProvider = new EmptyDragVisualProvider();
            private void ElementMouseLeave(object sender, MouseEventArgs e)
                var listboxItem = (sender as FrameworkElement).ParentOfType<RadListBoxItem>();
                if (listboxItem == null) return;
                DragDropManager.SetAllowCapturedDrag(listboxItem, true);
                var listbox = listboxItem.ParentOfType<RadListBox>();
                listbox.DragVisualProvider = new DragVisualProvider { DraggedItemTemplate = DragDataTemplate };
  4. Leland
    Leland avatar
    2 posts
    Member since:
    May 2016

    Posted 06 Nov 2018 in reply to George Link to this post

    Is this still the preferred way to handle this scenario?
  5. Vladimir Stoyanov
    Vladimir Stoyanov avatar
    602 posts

    Posted 09 Nov 2018 Link to this post

    Hello Leland,

    Indeed the demonstrated in this thread approach is the way to go to achieve the custom behavior in question. I do hope that such an approach is suitable for your scenario.

    That said, if it is not, or you are having troubles with the implementation, feel free to open a new support ticket and describe your scenario. 

    Vladimir Stoyanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top