Listbox Custom DragDropBehavior

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

    Posted 24 Feb 2016 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
    692 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.

    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. Trevor
    Trevor avatar
    1 posts
    Member since:
    Aug 2012

    Posted 31 May in reply to Nasko Link to this post

    Is there any way to do this without newing up the viewmodel in the code behind? We are using Caliburn and the viewmodels have numerous dependencies so its not practical to new them up.
  4. Martin Vatev
    Admin
    Martin Vatev avatar
    129 posts

    Posted 05 Jun Link to this post

    Hi Trevor,

    I prepared an example to demonstrate how to achieve the desired behavior. Please take a look at the implementation and consider how this approach fits your scenario.

    I hope that this helps.

    Regards,
    Martin Vatev
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top