private void puzzles_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
dragSource = parent;
//MessageBox.Show(e.GetPosition(parent).X.ToString());
//MessageBox.Show(e.GetPosition(parent).Y.ToString());
object data = GetDataFromListBox(dragSource, e.GetPosition(parent));
if (data != null)
{
DragDrop.DoDragDrop(parent, parent.Items.GetItemAt(2), DragDropEffects.Move);
}
}
private static object GetDataFromListBox(ListBox source, Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if (element == null)
{
MessageBox.Show("空的!");
return null;
}
else
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
//data = source.ItemContainerGenerator.ItemFromContainer(element);
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
if (element == source)
{
return null;
}
}
if (data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
puzzles是个ListBox,它的项是一个img对象
data = source.ItemContainerGenerator.ItemFromContainer(element);老是报不能为空,
请高手指教一下!
不能传递element进去,应该是你Binding的数据才是.
这句话的意思是,获取Binding的对象在UI上呈现的对象.