This is a migrated thread and some comments may be shown as answers.

Cannot get DragDrop events to fire

6 Answers 188 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 2
Charles asked on 29 Jan 2009, 09:01 PM
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.

6 Answers, 1 is accepted

Sort by
0
ankit
Top achievements
Rank 1
answered on 30 Jan 2009, 12:14 AM
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.
0
Charles
Top achievements
Rank 2
answered on 30 Jan 2009, 01:36 PM
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.
0
Pablo
Top achievements
Rank 2
answered on 31 Jan 2009, 03:39 AM
Can you try with this in the constructor?

RadDragAndDropManager.SetAllowDrag(this, true);  
0
Charles
Top achievements
Rank 2
answered on 02 Feb 2009, 02:32 PM
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.
0
Accepted
Pablo
Top achievements
Rank 2
answered on 02 Feb 2009, 03:11 PM
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);  
 
        }
0
Charles
Top achievements
Rank 2
answered on 02 Feb 2009, 04:16 PM
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.
Tags
DragAndDrop
Asked by
Charles
Top achievements
Rank 2
Answers by
ankit
Top achievements
Rank 1
Charles
Top achievements
Rank 2
Pablo
Top achievements
Rank 2
Share this question
or