Don't drag when selecting text with mouse

3 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()
            {
                InitializeComponent();
     
                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; }
        }
    }

    <Window
        x:Class="ListBoxTest.MainWindow"
        Title="MainWindow" Height="500" Width="525">
        <telerik:RadListBox
            ItemsSource="{Binding MyCollection}">
            <telerik:RadListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Height="50" Width="100" Background="LightBlue" telerik:DragDropManager.AllowCapturedDrag="True">
                        <TextBox
                            Text="{Binding .}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            telerik:DragDropManager.AllowCapturedDrag="False"
                            telerik:DragDropManager.AllowDrag="False"/>
                    </Grid>
                </DataTemplate>
            </telerik:RadListBox.ItemTemplate>
            <telerik:RadListBox.DragDropBehavior>
                <telerik:ListBoxDragDropBehavior />
            </telerik:RadListBox.DragDropBehavior>
        </telerik:RadListBox>
    </Window>
  2. Answer
    George
    Admin
    George avatar
    1332 posts

    Posted 22 Aug 2012 Link to this post

    Hello,

    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,
    George
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. 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
            (
                "DragDataTemplate",
                typeof(DataTemplate),
                typeof(NoDragBehavior),
                null
            );
     
            protected override void OnAttached()
            {
                base.OnAttached();
     
                Element.PreviewMouseLeftButtonDown += ElementOnPreviewMouseLeftButtonDown;
                Element.MouseLeave += ElementMouseLeave;
            }
     
            protected override void OnDetaching()
            {
                base.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 };
            }
        }
Back to Top