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

Listbox Custom DragDropBehavior

5 Answers 233 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Roman
Top achievements
Rank 1
Roman asked on 24 Feb 2016, 08:28 AM

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?

5 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 26 Feb 2016, 09:09 AM
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
0
Mehdi
Top achievements
Rank 1
answered on 01 Jun 2017, 01:08 AM
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.
0
Martin
Telerik team
answered on 05 Jun 2017, 12:04 PM
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 you to write beautiful native mobile apps using a single shared C# codebase.
0
Sathya
Top achievements
Rank 1
answered on 08 Mar 2018, 11:47 AM

Hi Martin,

Still you have used StaticResource in your sample, how we can implement if we used AutoWireViewModel through prism?

Regards,

Sathya

0
Martin
Telerik team
answered on 12 Mar 2018, 12:32 PM
Hello Sathya,

Can you take a look at this article where you can find three ways to simulate having an inheritance context for DependencyObjects external to an element tree? I think that the third way will help you to create a static resource from your ViewModel and use it in the binding of the command.

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 you to write beautiful native mobile apps using a single shared C# codebase.
Tags
ListBox
Asked by
Roman
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Mehdi
Top achievements
Rank 1
Martin
Telerik team
Sathya
Top achievements
Rank 1
Share this question
or