Bonjour,
Débutant avec XAML/WPF, je patauge totalement avec un binding sur un UserControl.
Contexte :
J'ai un UserControl que je peux déplacer librement (drag&drop) dans un canvas de la fenêtre principale. Ce UserControl possède une propriété 'maProprieteX' que je souhaite récupérer dans la fenêtre principale via une TextBox ; et c'est là que ce pose le problème ! Comment faire ??
Malgré mes nombreux tests aucun n'a été concluant...![]()
Code de Window1.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 <?xml version="1.0" encoding="utf-8"?> <Window x:Class="MonEspace.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:TEST_14.MonEspace" Title="TEST_14" Height="300" Width="300"> <Window.Resources> </Window.Resources> <Grid> <Canvas> <TextBox Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="28,46,25,0" Text="Je souhaite récupérer la propriété 'maProprieteX' de UserControl1" Width="237" Height="61" /> <Thumb Name="myThumb" DragCompleted="OnDragComplete" DragStarted="onDragStarted" DragDelta="OnDragDelta" AllowDrop="True" > <Thumb.Template> <ControlTemplate> <s:UserControl1 x:Name="TOTO" Width="100" Height="100" maProprieteX="66"> </s:UserControl1> </ControlTemplate> </Thumb.Template> </Thumb> </Canvas> </Grid> </Window>
Code de Window1.xaml.vb :
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 Imports System Imports System.Collections.Generic Imports System.Text Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Data Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Imaging Imports System.Windows.Shapes Imports System.Diagnostics Imports System.Windows.Controls.Primitives Namespace MonEspace ''' <summary> ''' Interaction logic for Window1.xaml ''' </summary> Public Partial Class Window1 Inherits Window Private _x As Double Private _y As Double Private _isDragging As Boolean Public Sub New() InitializeComponent() End Sub Private Sub OnDragStarted(ByVal sender As Object, ByVal e As DragStartedEventArgs) _isDragging = True _x = CDbl(GetValue(Canvas.LeftProperty)) _y = CDbl(GetValue(Canvas.TopProperty)) End Sub Private Sub OnDragDelta(ByVal sender As Object, ByVal e As DragDeltaEventArgs) ' En dehors d'un drag and drop on sort If Not _isDragging Then Return End If ' On récupère les nouvelles coordonnées _x += e.HorizontalChange _y += e.VerticalChange myThumb.SetValue(Canvas.LeftProperty, _x) myThumb.SetValue(Canvas.TopProperty, _y) End Sub Private Sub OnDragComplete(ByVal sender As Object, ByVal e As DragCompletedEventArgs) _isDragging = False End Sub End Class End Namespace
Code de UserControl1.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 <UserControl x:Class="MonEspace.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:TEST_14.MonEspace"> <Grid> <Rectangle x:Name="MonRectangle" Fill="AliceBlue" Opacity="0.8" maProprieteX="1234" Width="Auto" Height="auto"> </Rectangle> </Grid> </UserControl>
Code de UserControl1.xaml.vb :
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 Imports System Imports System.Collections.Generic Imports System.Text Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Data Imports System.Windows.Documents Imports System.Windows.Input Imports System.Windows.Media Imports System.ComponentModel Namespace MonEspace ''' <summary> ''' Interaction logic for UserControl1.xaml ''' </summary> Public Partial Class UserControl1 Inherits UserControl Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Shared ReadOnly maProprieteXProperty As DependencyProperty = DependencyProperty.Register("maProprieteX", GetType(Double), GetType(UserControl1)) Private xx As Double = 0 Public Sub New() InitializeComponent() End Sub Public Property maProprieteX As Double Get Return xx End Get Set If xx = value Then Return End If xx = value RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("maProprieteX")) End Set End Property End Class End Namespace
Je remercie par avance tous ceux qui pourront m'aiguiller vers une solution !
Seb.
Partager