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

RadListBox drag-n-drop limited amount of items

1 Answer 46 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Kirill
Top achievements
Rank 1
Kirill asked on 02 Mar 2014, 02:23 PM
Hi there. I've met the following challenge with RadListBox (RLB) control. I need to implement Drag-n-Drop feature beetween two RLBs, where the destination RLB must contain up to 6 elements and they must be reordable. I used DragDropEffects property in proper condition using, it works OK for me, but if I add all 6 items to the destination RLB I cannot reorder them, meanwhile RLB with up to 5 items is reordable. Could you please assist with it? Thanks in an advance.

1 Answer, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 05 Mar 2014, 11:42 AM
Hello Kirill,

You probably couldn't reorder items because the drop is disabled when the items count is six. This also prevents the drop when reordering the items. I can suggest you an approach using a custom ListBoxDragDropBeahavior which also check is if the dragging has started from the same control. You will need to override the CanDrop method the following way:

public class CustomListBoxDragDropBehavior : ListBoxDragDropBehavior
{
    public override bool CanDrop(DragDropState state)
    {
             
        if (state.DestinationItemsSource.Count > 5 && !state.IsSameControl)
        {
            return false;
        }
             
 
        return base.CanDrop(state);
    }
}

The XAML should look the following way:

<Window.Resources>
    <Style TargetType="telerik:RadListBoxItem">
        <Setter Property="telerik:DragDropManager.AllowCapturedDrag" Value="True" />
    </Style>
</Window.Resources>
 
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150" />
        <ColumnDefinition Width="150" />           
    </Grid.ColumnDefinitions>
    <telerik:RadListBox Grid.Column="0" AllowDrop="True" ItemsSource="{Binding ItemsSource1}" Margin="10">
        <telerik:RadListBox.DragDropBehavior>
            <local:CustomListBoxDragDropBehavior />
        </telerik:RadListBox.DragDropBehavior>
    </telerik:RadListBox>
    <telerik:RadListBox Grid.Column="1" AllowDrop="True" ItemsSource="{Binding ItemsSource2}" Margin="10">
        <telerik:RadListBox.DragDropBehavior>
            <local:CustomListBoxDragDropBehavior />
        </telerik:RadListBox.DragDropBehavior>
    </telerik:RadListBox>
</Grid>

Hope this helps.

Regards,
Kalin
Telerik

DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

Tags
ListBox
Asked by
Kirill
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Share this question
or