首页 新闻 会员 周边 捐助

哪位高手帮我看看这个方法的调用怎么出错了?

0
悬赏园豆:20 [已关闭问题]

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);老是报不能为空,

请高手指教一下!

kaiz的主页 kaiz | 初学一级 | 园豆:71
提问于:2010-07-19 13:54
< >
分享
其他回答(1)
0

不能传递element进去,应该是你Binding的数据才是.

这句话的意思是,获取Binding的对象在UI上呈现的对象.

Franz | 园豆:495 (菜鸟二级) | 2010-07-25 12:47
0

http://blog.sina.com.cn/s/blog_685790700100ltmh.html我也曾遇到这样的问题,这篇博文讲的很详细,你看看吧,特别介绍了为什么是null以及解决方案

iSpring | 园豆:205 (菜鸟二级) | 2011-05-22 21:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册