Listbox Custom DragDropBehavior

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

    Posted 24 Feb Link to this post

    Hi!

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

    <UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="MyProject.Views.MyView"
                         ....
                         xmlns:behaviors="clr-namespace:MyProject.Behaviors"
                         
    mc:Ignorable="d"
                         d:DesignHeight="300"
                         d:DesignWidth="300">
    <Grid>
              ...
               <telerik:RadListBox ItemsSource="{Binding Items}"
                             <telerik:RadListBox.DragDropBehavior>
                                           <behaviors:MyDragDropBehavior AllowReorder="True" DropCommand="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.DropCommand}"/>
                             </telerik:RadListBox.DragDropBehavior>
               </telerik:RadListBox>
    </Grid>
    </UserControl>

    View gets viewmodel via injection

    public partial class MyView : UserControl
    {
                       public MyView (ViewModels.MyViewModel viewModel)
                       {
                                    InitializeComponent();
                                    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)
                      {
                                      base.Drop(state);
                                      DropCommand.Execute(null);
                      }
                     
                     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
    Admin
    Nasko avatar
    586 posts

    Posted 26 Feb 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.

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