1

I had created a drag and drop control in wpf to drag and drop data between two list boxes which worked as a charm until I moved it to another project.

The difference being It was initially a wpf window and used the window object to get the mouse position and the position of the controls inside.

this.topWindow = Window.GetWindow(this.sourceItemsControl); //Source items control is instance of ItemsControl

bool previousAllowDrop = this.topWindow.AllowDrop;
this.topWindow.AllowDrop = true;

and now I had to change it to a user control instead, since its a part of a bigger project which is a Windows forms project and the views are linked as a Smart Part from the main project. So now the Window object is null.

I looked for a similar functionality for User Control but could not find it..What is it that I am missing out?? I know there should be something..Would appreciate any help on the same..

P.S. : I am using the MVVM architecture

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user1521554
  • 117
  • 3
  • 14
  • Have you checked this one: [http://stackoverflow.com/questions/302839/wpf-user-control-parent](http://stackoverflow.com/questions/302839/wpf-user-control-parent) – ekholm Aug 09 '12 at 15:24
  • yea..Was using a similar method, doesnt work..returns a null always.. – user1521554 Aug 15 '12 at 19:04
  • Thanks ekholm for the heads up, The link gave the hint. Have answered the question below. – user1521554 Aug 15 '12 at 20:21

2 Answers2

1

Found the way to find the base User control using recursion, Thanks to ekholm for the heads up..

public static UserControl FindParentControl(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            //CHeck if this is the end of the tree
            if (parent == null) return null;

            UserControl parentControl = parent as UserControl;
            if (parentControl != null)
            {
                return parentControl;
            }
            else
            {
                //use recursion until it reaches a Window
                return FindParentControl(parent);
            }
        } 

Now this base user control can be used to find the coordinates (reference) as well as setting other properties like AllowDrop, DragEnter, DragOver etc.

user1521554
  • 117
  • 3
  • 14
-1

If you need MVVM, than you may examine this solution: in your .xaml file add:

<ContentControl Content="{Binding Content, Mode=TwoWay}" AllowDrop="True" Name="myDesignerContentControl" />

Than in your ViewModel add the following:

private Panel _content;
    public Panel Content {
        get { return _content; }
        set {
            _content = value;
            if (_content != null) {
                RegisterDragAndDrop();
            }
            base.RaisePropertyChanged("Content");
        }
    }
private void RegisterDragAndDrop() {
    Content.Drop += OnDrop;
    Content.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
    Content.PreviewDragOver += OnDragOver;
}

private void OnDesignerDrop(object sender, DragEventArgs e) {
    //some custom logic handling
}
private void OnDesignerMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    var control = (FrameworkElement)e.Source;
    //some custom logic handling for doing drag & drop
}
private void OnDesignerDragOver(object sender, DragEventArgs e) {
    //some custom logic handling for doing drag over
}

The idea is that you should use controls instead of mouse positions, it will be more simple and logical approach. The code above is an example of approach to use in MVVM for having an content area on which you may perform drag and drop of some controls. The idea behind is also suitable for drag and drop data between two list boxes, that you may have on the same content area.

Hope this helps.

diadiora
  • 599
  • 3
  • 13