Listbox Custom DragDropBehavior

2 posts, 0 answers
  1. Roman
    Roman avatar
    1 posts
    Member since:
    Mar 2013

    Posted 24 Feb 2016 Link to this post


    I'm trying to use binding in DragDropBehavior of RadListBox like this

    <UserControl xmlns:telerik="" x:Class="MyProject.Views.MyView"
               <telerik:RadListBox ItemsSource="{Binding Items}"
                                           <behaviors:MyDragDropBehavior AllowReorder="True" DropCommand="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.DropCommand}"/>

    View gets viewmodel via injection

    public partial class MyView : UserControl
                       public MyView (ViewModels.MyViewModel viewModel)
                                    DataContext = viewModel;

    Behavior code:

    public class MyDragDropBehavior : Telerik.Windows.DragDrop.Behaviors.ListBoxDragDropBehavior
                       public override bool CanDrop(Telerik.Windows.DragDrop.Behaviors.DragDropState state)
                                        return state.IsSameControl;
                     public override void Drop(Telerik.Windows.DragDrop.Behaviors.DragDropState state)
                     public ICommand DropCommand
                                   get { return (ICommand)GetValue(DropCommandProperty); }
                                   set { SetValue(DropCommandProperty, value); }
                      public static readonly DependencyProperty DropCommandProperty =DependencyProperty.Register("DropCommand", typeof(ICommand), typeof(MyDragDropBehavior), new PropertyMetadata(null));

    Items binding is working well. Behavior is working but binding to DropCommand is not. I obtain binding error:

    Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.DropCommand; DataItem=null; target element is 'MyDragDropBehavior' (HashCode=25707777); target property is 'DropCommand' (type 'ICommand')

    ViewModel is

    public class MyViewModel
                         public MyViewModel()
                                          DropCommand = new DelegateCommand(OnDrop);
                                          Items = new ObservableCollection<MyItem>();
                         public ObservableCollection<MyItem> Items { get; set; }
                         public DelegateCommand DropCommand { get; set; }

                         private void OnDrop(){}

    What is wrong?

  2. Nasko
    Nasko avatar
    665 posts

    Posted 26 Feb 2016 Link to this post

    Hi Roman,

    In order to achieve the desired binding for that concrete scenario you need to add the DataContext inside the Resources of the UserControl and get access to the Command from that StaticResource.

    Please, check the attached sample that demonstrates that approach.

    Hope this helps.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top