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

Drag and Drop within ListBox with databound SelectedItem causes null set

1 Answer 144 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 27 Aug 2013, 01:30 PM
Hello,

I am trying to get Drag and Drop to work within a single RadListBox.  I have the ItemsSource and SelectedItem bound (TwoWay).  Whenever I drop an item within the listbox, the SelectedItem setter gets called with a value of null.  I was able to reproduce this by updating the SDK sample "CustomListBoxDragDropBehavior_WPF" with the following:

<telerik:RadListBox ItemContainerStyle="{StaticResource DraggableListBoxItem}"
                            ItemsSource="{Binding Customers1}"
                            SelectedItem="{Binding SelectedCustomer1, Mode=TwoWay}"
private Customer selectedCustomer1;
        public Customer SelectedCustomer1
        {
            get { return selectedCustomer1; }
            set
            {
                selectedCustomer1 = value;
                this.OnPropertyChanged(() => this.SelectedCustomer1);
            }
        }

How do I prevent the selected item to be called with null?

Thanks,
Steve

1 Answer, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 28 Aug 2013, 12:00 PM
Hi Steve,

The SelectedItem is being set to null because of the ListBox DradDragBehavior implementation. Whenever the dragged ListItem is dropped it has been removed from the ItemsSource of the first ListBox and added to the ItemsSource of the second ListBox. So in order to achieve the desired scenario in a single ListBox I would suggest a quick solution. Just use our CustomListBoxDragDropBehavior SDK example as a base and add the code snippet below:
public MainWindow()
{
    InitializeComponent();
    DragDropManager.AddDropHandler(this.lb1, OnDrop, true);
}
 
private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
{
    var data = ((DataObject)e.Data).GetData(typeof(Customer));
    this.lb1.SelectedItem = ((IList)data)[0];
}

This will extend the Drop handler of the DragDropManager in order to set the dropped ListItem as the SelectedItem (you would only need to set the name of the first ListBox to "lb1").

Hope this helps.

Regards,
Kalin
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
DragAndDrop
Asked by
Steve
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Share this question
or