RadListBox DragDropBehavior Binding

330 Views Asked by At

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;
        }
    }

Behaviour 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?

I have found a way to solve the issue by following

<telerik:RadListBox DragDropBehavior="{Binding DragDropBehavior}">

But I still don't understand why previous method doesn't work. I'll appreciate if somebody knows.

0

There are 0 best solutions below