Drag and Drop within ListBox with databound SelectedItem causes null set

2 posts, 0 answers
  1. Steve
    Steve avatar
    8 posts
    Member since:
    Jun 2013

    Posted 27 Aug 2013 Link to this post

    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
  2. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 28 Aug 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top