IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Problème de binding avec UserControl


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 4
    Par défaut Problème de binding avec UserControl
    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.

  2. #2
    Membre Expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Par défaut
    A première vue, une solution serait du binder maProprieteX à une propriété du ViewModel. Et de binder cette propriété à ta textbox.

Discussions similaires

  1. problème de binding avec un controle utilisateur
    Par EmacLi dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 06/10/2009, 21h17
  2. C# Net - Problème de Binding avec DataSet
    Par Anified dans le forum C#
    Réponses: 0
    Dernier message: 21/09/2009, 02h00
  3. Problème de Binding avec un formulaire
    Par vilveq dans le forum Flex
    Réponses: 7
    Dernier message: 06/08/2009, 11h03
  4. Problème de Binding avec un UserControl
    Par birdyz dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 20/02/2009, 12h31
  5. problème de bindings avec DropDownList
    Par Vlatiska dans le forum ASP.NET
    Réponses: 14
    Dernier message: 17/03/2007, 14h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo