Bonjour,

J'ai créé un système de drag & drop avec un datagrid de mon application (WPF) avec le code suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public delegate Point GetPosition(IInputElement element);
 
 private void dataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int indexDrag = -1;
            try
            {
                GetCurrentRowIndex(e.GetPosition);
            }
            catch
            {
                indexDrag = -1;
            }
 
 
            if(indexDrag >= 0)
            {
                _startPoint = e.GetPosition(null);
            }
 
        }
        private void dataGrid1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)
                {
                    Point position = e.GetPosition(null);
 
                    if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                        Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                    {
                        StartDrag(e);
 
 
                    }
                }
            }
            catch { }
        }
        private void StartDrag(MouseEventArgs e)
        {
            try
            {
                IsDragging = true;
                indexDrag = GetCurrentRowIndex(e.GetPosition);
 
                Point position = e.GetPosition(null);
                // this.photo.Margin = new Thickness(position.X, position.Y, 0, 0);
                // this.photo.Source = calculs.GetImageSourceFromString(_Data[indexDrag].Joueur.GetLinkImage());
 
                DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), indexDrag);
                DragDropEffects de = DragDrop.DoDragDrop(this.dataGrid1, data, DragDropEffects.Move);
 
                IsDragging = false;
            }
            catch
            {
 
            }
        }
 
        private int GetCurrentRowIndex(GetPosition pos)
        {
            int curIndex = -1;
            for (int i = 0; i < _Data.Count; i++)
            {
                DataGridRow itm = GetRowItem(i);
                if (GetMouseTargetRow(itm, pos))
                {
                    curIndex = i;
                    break;
                }
            }
            return curIndex;
        }
        private bool GetMouseTargetRow(Visual theTarget, GetPosition position)
        {
            Rect rect = VisualTreeHelper.GetDescendantBounds(theTarget);
            Point point = position((IInputElement)theTarget);
            return rect.Contains(point);
        }
        private DataGridRow GetRowItem(int index)
        {
            if (this.dataGrid1.ItemContainerGenerator.Status
                    != GeneratorStatus.ContainersGenerated)
                return null;
            return dataGrid1.ItemContainerGenerator.ContainerFromIndex(index)
                                                            as DataGridRow;
        }
Il fonctionne très bien sauf pour un point :
- dés que je clique sur la scrollbar le drag & drop plante et ne fonctionne plus ensuite. (j'ai essayé de mettre un try/catch au moment où l'on récupère l'index).

Dés que je clique sur la scrollbar (indexdrag vaut -1 suite au catch, mais si je reclique ensuite la ligne Rect rect = VisualTreeHelper.GetDescendantBounds(theTarget); ne fonctionne plus.

L'idée serait peut etre de détecter après avoir fait le GetPosition si ça renvoie une position associée à la scrollbar ?

Merci d'avance pour votre aide.