Cannot get DragDrop events to fire

7 posts, 1 answers
  1. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 29 Jan 2009 Link to this post

    I am attempting to get the RadDragAndDrop manager working between two listboxes. I have used the sample code provided in the Examples solution as a guide. Basically the only difference between my code and the code in the example is the data being handled. I am getting a list of custom objects using WCF calls instead of hardcoding the list.

    I have wired up all events as in the example, have put breakpoints on all of them, but they never fire. Here is a sample of the code:
     
        public class DraggableListBox : ListBox  
        {  
            Brush listBoxDefaultBrush;  
            Brush listBoxDragPossible = new SolidColorBrush(Colors.Orange);  
     
            public event EventHandler<DragDropQueryEventArgs> DragQuery  
            {  
                add  
                {  
                    this.AddHandler(RadDragAndDropManager.DropQueryEvent, value);  
                }  
                remove  
                {  
                    this.RemoveHandler(RadDragAndDropManager.DragQueryEvent, value);  
                }  
            }  
     
            public event EventHandler<DragDropQueryEventArgs> DropQuery  
            {  
                add  
                {  
                    this.AddHandler(RadDragAndDropManager.DropQueryEvent, value);  
                }  
                remove  
                {  
                    this.RemoveHandler(RadDragAndDropManager.DropQueryEvent, value);  
                }  
            }  
     
            public event EventHandler<DragDropEventArgs> DragInfo  
            {  
                add  
                {  
                    this.AddHandler(RadDragAndDropManager.DragInfoEvent, value);  
                }  
                remove  
                {  
                    this.RemoveHandler(RadDragAndDropManager.DragInfoEvent, value);  
                }  
            }  
     
            public event EventHandler<DragDropEventArgs> DropInfo  
            {  
                add  
                {  
                    this.AddHandler(RadDragAndDropManager.DropInfoEvent, value);  
                }  
                remove  
                {  
                    this.RemoveHandler(RadDragAndDropManager.DropInfoEvent, value);  
                }  
            }  
     
            public DraggableListBox()  
            {  
                RadDragAndDropManager.SetAllowDrop(this, true);  
     
                this.DragQuery += new EventHandler<DragDropQueryEventArgs>(OnDragQuery);  
                this.DragInfo += new EventHandler<DragDropEventArgs>(OnDragInfo);  
                this.DropQuery += new EventHandler<DragDropQueryEventArgs>(OnDropQuery);  
                this.DropInfo += new EventHandler<DragDropEventArgs>(OnDropInfo);  
     
            }  
     
            public override void OnApplyTemplate()  
            {  
                base.OnApplyTemplate();  
                listBoxDefaultBrush = BorderBrush;  
            }  
     
            void OnDropInfo(object sender, DragDropEventArgs e)  
            {  
                DraggableListBox box = sender as DraggableListBox;  
                IList<RuleDTO> itemsSource = box.ItemsSource as IList<RuleDTO>;  
                RuleDTO payload = e.Options.Payload as RuleDTO;  
     
                if (e.Options.Status == DragStatus.DropPossible)  
                {  
                    box.BorderBrush = listBoxDragPossible;  
                }  
                else  
                {  
                    box.BorderBrush = listBoxDefaultBrush;  
                }  
     
                if (e.Options.Status == DragStatus.DropComplete)  
                {  
                    if (!itemsSource.Contains(payload))  
                    {  
                        itemsSource.Add(payload);  
                    }  
                }  
            }  
     
            void OnDropQuery(object sender, DragDropQueryEventArgs e)  
            {  
                DraggableListBox box = sender as DraggableListBox;  
                IList<RuleDTO> itemsSource = box.ItemsSource as IList<RuleDTO>;  
                RuleDTO payload = e.Options.Payload as RuleDTO;  
     
                e.QueryResult = payload != null && !itemsSource.Contains(payload);  
            }  
     
            void OnDragInfo(object sender, DragDropEventArgs e)  
            {  
                DraggableListBox box = sender as DraggableListBox;  
                IList<RuleDTO> itemsSource = box.ItemsSource as IList<RuleDTO>;  
                RuleDTO payload = e.Options.Payload as RuleDTO;  
     
                if (e.Options.Status == DragStatus.DragComplete)  
                {  
                    if (payload != null && itemsSource.Contains(payload))  
                    {  
                        itemsSource.Remove(payload);  
                    }  
                }  
            }  
     
            protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)  
            {  
                DraggableListBox box = sender as DraggableListBox;  
     
                e.QueryResult = true;  
                if (e.Options.Status == DragStatus.DragQuery)  
                {  
                    e.Options.SourceCueHost = null;  
                    e.Options.Payload = box.SelectedItem;  
                    ContentControl cue = new ContentControl();  
                    //cue.ContentTemplate = this.Resources["ApplicationDragTemplate"] as DataTemplate;  
                    cue.Content = box.SelectedItem;  
                    e.Options.DragCue = cue;  
                    e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();  
                }  
            }  
     
            protected override void PrepareContainerForItemOverride(DependencyObject element, object item)  
            {  
                base.PrepareContainerForItemOverride(element, item);  
                RadDragAndDropManager.SetAllowDrag(element, true);  
                RadDragAndDropManager.SetAllowDrop(element, true);  
            }  
     
            protected override void ClearContainerForItemOverride(DependencyObject element, object item)  
            {  
                base.ClearContainerForItemOverride(element, item);  
                RadDragAndDropManager.SetAllowDrag(element, false);  
                RadDragAndDropManager.SetAllowDrop(element, false);  
            }  
        } 

    Here is the XAML

            <local:DraggableListBox x:Name="lboAvailableRules" Grid.Column="1" Grid.Row="4" Grid.RowSpan="3" Margin="8,8,30,8">  
                <local:DraggableListBox.ItemContainerStyle> 
                    <Style TargetType="ListBoxItem">  
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>  
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
                        <Setter Property="DragDrop:RadDragAndDropManager.AllowDrop" Value="True"/>  
                        <Setter Property="DragDrop:RadDragAndDropManager.AllowDrag" Value="True"/>  
                    </Style> 
                </local:DraggableListBox.ItemContainerStyle> 
                <local:DraggableListBox.ItemTemplate> 
                    <DataTemplate> 
                        <TextBlock Text="{Binding RuleName}" FontWeight="Bold" HorizontalAlignment="Left" /> 
                    </DataTemplate> 
                </local:DraggableListBox.ItemTemplate> 
            </local:DraggableListBox> 
     
            <local:DraggableListBox x:Name="lboSelectedRules" Margin="8,8,30,8" Grid.Column="3" Grid.Row="4" Grid.RowSpan="3">  
                <local:DraggableListBox.ItemTemplate> 
                    <DataTemplate> 
                        <TextBlock Text="{Binding RuleName}" HorizontalAlignment="Center" /> 
                    </DataTemplate> 
                </local:DraggableListBox.ItemTemplate> 
            </local:DraggableListBox> 

    I can't seem to get it to work.

    The listboxes are within a grid (as you can see from the XAML. Is that a problem?

    Thanks in advance for any help.
  2. ankit
    ankit avatar
    29 posts
    Member since:
    Sep 2008

    Posted 29 Jan 2009 Link to this post

    What is your source for dragable list box.
    Make sure you have source for both and It should be observable collection, not list.

    See how you go.
  3. DevCraft banner
  4. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 30 Jan 2009 Link to this post

    Thanks, but that didn't work. Here is my "GetRulesCompleted" method which fires when the data is retrieved from the WCF service call.

            private ObservableCollection<RuleDTO> availableRules;  
            private ObservableCollection<RuleDTO> selectedRules;  
     
            public void GetRulesCompleted(object sender, GetRulesCompletedEventArgs e)  
            {  
                if (e.Error != null)  
                {  
                    MessageBox.Show(e.Error.Message);  
                }  
                else  
                {  
                    availableRules = new ObservableCollection<RuleDTO>();  
                    foreach (var dto in e.Result)  
                    {  
                        availableRules.Add(dto);  
                    }  
     
                    lboAvailableRules.ItemsSource = availableRules;  
                    lboSelectedRules.ItemsSource = selectedRules;  
                }  
     
            } 

    I have defined two ObservableCollections of type RuleDTO. During the GetRulesCompleted call, I translate the returned List<RuleDTO> into a corresponding ObservableCollection. Then I set the ItemsSource of both listboxes. One to a populated ObservableCollection and the other to a null ObservableCollection. The lboAvailableRules listbox is the source of items to be dragged to the lboSelectedRules listbox.

    Once again - the dragdrop events do not fire - I have break points in all of them.
  5. Pablo
    Pablo avatar
    5 posts
    Member since:
    Nov 2008

    Posted 30 Jan 2009 Link to this post

    Can you try with this in the constructor?

    RadDragAndDropManager.SetAllowDrag(this, true);  
  6. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 02 Feb 2009 Link to this post

    Sorry, that didn't work either.

    I have that call in the constructor for the user control and in the constructor for the "DraggableListBox". Nothing seems to work.
  7. Answer
    Pablo
    Pablo avatar
    5 posts
    Member since:
    Nov 2008

    Posted 02 Feb 2009 Link to this post

    This is your posted code:

            public event EventHandler<DragDropQueryEventArgs> DragQuery  

            {  

                add  

                {  

                    this.AddHandler(RadDragAndDropManager.DropQueryEvent, value);  

                }  

                remove  

                {  

                    this.RemoveHandler(RadDragAndDropManager.DragQueryEvent, value);  

                }  

            }  


    try replacing by
     this.AddHandler(RadDragAndDropManager.DragQueryEvent, value);  

    And here...

            public DraggableListBox()  
            {  
                RadDragAndDropManager.SetAllowDrop(this, true);  
                RadDragAndDropManager.SetAllowDrag(this, true);  
     
                this.DragQuery += new EventHandler<DragDropQueryEventArgs>(OnDragQuery);  
                this.DragInfo += new EventHandler<DragDropEventArgs>(OnDragInfo);  
                this.DropQuery += new EventHandler<DragDropQueryEventArgs>(OnDropQuery);  
                this.DropInfo += new EventHandler<DragDropEventArgs>(OnDropInfo);  
     
            }
  8. Charles
    Charles avatar
    75 posts
    Member since:
    Aug 2008

    Posted 02 Feb 2009 Link to this post

    Thanks very much, Pablo.

    That did it.  I thought I had copied code directly from the sample - but I must have missed something.

    I had a small logic problem after that with the ItemsSource of the receiving listbox being null, but I fixed that and it works fine.

    Thanks again.
Back to Top
DevCraft banner