rodriwasabi / gong-wpf-dragdrop

Automatically exported from code.google.com/p/gong-wpf-dragdrop
0 stars 1 forks source link

No support for derived item container classes #30

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I have a ListBox which uses a custom item container class (derived from 
ListBoxItem). After enabling drag and drop for this ListBox (using 
dd:DragSource = "true" dd:DropTarget = "true") selecting any item was not 
possible anymore.

The problem is caused by, GetItemContainer not finding my custom derived item 
container class.

I'd recommend changing GetVisualAncestor not to use "==" for type checking, but 
using something which is comparable to the "is" operator:

public static DependencyObject GetVisualAncestor(this DependencyObject d, Type 
type)
        {
            DependencyObject item = VisualTreeHelper.GetParent(d);

            while (item != null)
            {
                if (item.GetType() == type ||
                    item.GetType().IsSubclassOf(type)) 
                    return item;
                item = VisualTreeHelper.GetParent(item);
            }

            return null;
        }

After this change the problem is gone.

Original issue reported on code.google.com by Jan_Waec...@gmx.de on 16 Jan 2011 at 3:53