Bonjour,
J'ai codé un petit contrôle utilisateur qui devra être réutilisé par la suite dans mes applications. Je suis arrivé pratiquement au but fixé mais un bug persiste et je n'arrive pas à mettre la main dessus.
J'ai utiliser MS Expression blend 2.5 March preview et MS Visual Studio 2008, avec framework 3.5
fichier : DraggableUserControl.xaml.cs
fichier : DraggableUserControl.xaml
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MesControles { public partial class DraggableUserControl : UserControl { bool DragEnCours = false; Point DragOffset; public DraggableUserControl() { InitializeComponent(); // Evénements attachés this.MouseLeftButtonDown += DragMouseLeftButtonDown; this.MouseLeftButtonUp += DragMouseLeftButtonUp; this.MouseMove += DragMouseMove; // Position par rapport au control parent / haut, gauche base.HorizontalAlignment = HorizontalAlignment.Left; base.VerticalAlignment = VerticalAlignment.Top; } public void Move(Point Cible) { this.RenderTransform = new TranslateTransform(Cible.X, Cible.Y); } public void DragMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (DragEnCours) this.StopDrag(); DragEnCours = true; DragOffset = e.GetPosition(null); this.CaptureMouse(); } void DragMouseMove(object sender, MouseEventArgs e) { if (DragEnCours) this.DoDrag(e.GetPosition(this.Parent as UIElement)); } void DragMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (DragEnCours) this.StopDrag(); } void DoDrag(Point Cible) { this.Move(new Point(Cible.X - DragOffset.X, Cible.Y - DragOffset.Y)); } void StopDrag() { DragEnCours = false; this.ReleaseMouseCapture(); } } }
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 <UserControl x:Class="MesControles.DraggableUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="#FFFFFFFF" CornerRadius="8,8,8,8" BorderThickness="2,2,2,2"> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF000000" Offset="0"/> <GradientStop Color="#FFFFFFFF" Offset="1"/> </LinearGradientBrush> </Border.Background> </Border> </Grid> </UserControl>
En mode éxécution :
Le contrôle se déplace bien à la souris. Quand je relache le bouton gauche, il reste bien en place. Lorsque je re-clique dessus le contrôle, il reprends sa position initiale (celle mise dans le designer).
Si vous avez une idée, car à force d'avoir le nez collé dans le code, je n'arrive pas à trouver mon erreur ou oubli.
Capture d'écran ci-dessous.
Merci à vous
Philippe
Partager