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 :

binding formulaire et classe


Sujet :

Windows Presentation Foundation

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut binding formulaire et classe
    Bonjour,

    j'essaie désespérément de lier mon formulaire à une classe que j'ai conçu.

    mais je ne sais pas comment m'y prendre.

    voici ma classe.

    Code C# : 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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace Verification_Lotto
    {
        class Joueur
        {
            private string nom;
            private numeroLotto num1, num2, num3, num4, num5, num6;
     
            public Joueur(string p_nom, int p_num1, int p_num2, int p_num3, int p_num4, int p_num5, int p_num6)
            {
                nom = p_nom;
                num1.Numero = p_num1;
                num2.Numero = p_num2;
                num3.Numero = p_num3;
                num4.Numero = p_num4;
                num5.Numero = p_num5;
                num6.Numero = p_num6;
            }
            public Joueur()
            {
                nom = "";
                num1.Numero = num2.Numero = num3.Numero = num4.Numero = num5.Numero = num6.Numero = 0;
     
            }
            public string Nom
            {
                set
                {
                    nom = value;
                }
                get
                {
                    return nom;
                }
            }
            public numeroLotto Num1
            {
                set
                {
                    num1 = value;
                }
                get
                {
                    return num1;
                }
            }
            public numeroLotto Num2
            {
                set
                {
                    num2 = value;
                }
                get
                {
                    return num2;
                }
            }
            public numeroLotto Num3
            {
                set
                {
                    num3 = value;
                }
                get
                {
                    return num3;
                }
            }
            public numeroLotto Num4
            {
                set
                {
                    num4 = value;
                }
                get
                {
                    return num4;
                }
            }
            public numeroLotto Num5
            {
                set
                {
                    num5 = value;
                }
                get
                {
                    return num5;
                }
            }
            public numeroLotto Num6
            {
                set
                {
                    num6 = value;
                }
                get
                {
                    return num6;
                }
            }
        }
    }

    la classe numeroLotto ( à mon avis pas utile au binding )
    Code C# : 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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace Verification_Lotto
    {
        class numeroLotto
        {
     
            int numero;
            bool tire;
     
            public numeroLotto(int p_numero, bool p_tire)
            {
                numero = p_numero;
                tire = p_tire;
            }
     
            public numeroLotto()
            {
                numero = 0;
                tire = false;
            }
     
            public int Numero
            {
                set
                {
                    numero = value;
                }
                get
                {
                    return numero;
                }
            }
            public bool Tire
            {
                set
                {
                    tire = value;
                }
                get
                {
                    return tire;
                }
            }
        }
    }

    et voici le code XAML du formulaire auquel je veux binder ma classe.

    Code xaml : 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
     
    <Window x:Class="Verification_Lotto.AjoutJoueur"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:Verification_Lotto"
        Title="Ajout d'un nouveau joueur" Height="340" Width="800" Loaded="Window_Loaded">
     
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
     
            <Label Grid.Column="1" Grid.Row="1" VerticalAlignment="Center">Nom :</Label>
            <Label Grid.Column="1" Grid.Row="2" VerticalAlignment="Center">Numéro 1 :</Label>
            <Label Grid.Column="1" Grid.Row="3" VerticalAlignment="Center">Numéro 2 :</Label>
            <Label Grid.Column="1" Grid.Row="4" VerticalAlignment="Center">Numéro 3 :</Label>
            <Label Grid.Column="1" Grid.Row="5" VerticalAlignment="Center">Numéro 4 :</Label>
            <Label Grid.Column="1" Grid.Row="6" VerticalAlignment="Center">Numéro 5 :</Label>
            <Label Grid.Column="1" Grid.Row="7" VerticalAlignment="Center">Numéro 6 :</Label>
     
            <Grid Grid.Column="1" Grid.Row="8" Grid.ColumnSpan="2" >
     
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="150"/>
                </Grid.ColumnDefinitions>
     
                <Button Name="btAjouter" Grid.Column="1" Grid.Row="0" Content="Valider" Height="30" Width="150" Click="btAjouter_Click"></Button>
                <Button Name="btAnnuler" Grid.Column="2" Grid.Row="1" Content="Annuler" Height="30" Width="150" Click="btAnnuler_Click"></Button>
     
            </Grid>
     
            <TextBox Grid.Column="2" Grid.Row="1" Name="tbNom" Height="26" Width="280" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBox>
     
            <StackPanel Grid.Column="2" Grid.Row="2" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum1" Valeur="1"></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="3" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum2" Valeur="1" ></my:NumericUpDown>  
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="4" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum3" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="5" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum4" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="6" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum5" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="7" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum6" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
     
        </Grid>
    </Window>

    je veux donc lier ma textbox "tbnom.text" à Joueur.Nom, NUD1.Valeur à Joueur.numeroLotto.Numero, etc...

    Un grand merci.

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    J'ai essayé ceci pour la textbox.

    au niveau du code Behind j'ai créer un objet Joueur.
    et ai modifié la propriété Nom de mon Joueur.

    Code c# : 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
     
    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.Shapes;
     
    namespace Verification_Lotto
    {
        /// <summary>
        /// Interaction logic for AjoutJoueur.xaml
        /// </summary>
        public partial class AjoutJoueur : Window
        {
     
            Joueur joueur = new Joueur("", 1, 2, 3, 4, 5, 6);
     
            public AjoutJoueur()
            {
                InitializeComponent();
            }
     
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
     
            }
     
            private void btAjouter_Click(object sender, RoutedEventArgs e)
            {
                joueur.Nom = "moi";
            }
     
            private void btAnnuler_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
        }
    }

    au niveau du code Xaml j'ai binding la propriété Text de ma textbox au la propriété nom de mon joueur.

    Code xaml : 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
     
    Window x:Class="Verification_Lotto.AjoutJoueur"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:my="clr-namespace:Verification_Lotto"
        Title="Ajout d'un nouveau joueur" Height="340" Width="800" Loaded="Window_Loaded">
     
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition Height="30"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"/>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
     
            <Label Grid.Column="1" Grid.Row="1" VerticalAlignment="Center">Nom :</Label>
            <Label Grid.Column="1" Grid.Row="2" VerticalAlignment="Center">Numéro 1 :</Label>
            <Label Grid.Column="1" Grid.Row="3" VerticalAlignment="Center">Numéro 2 :</Label>
            <Label Grid.Column="1" Grid.Row="4" VerticalAlignment="Center">Numéro 3 :</Label>
            <Label Grid.Column="1" Grid.Row="5" VerticalAlignment="Center">Numéro 4 :</Label>
            <Label Grid.Column="1" Grid.Row="6" VerticalAlignment="Center">Numéro 5 :</Label>
            <Label Grid.Column="1" Grid.Row="7" VerticalAlignment="Center">Numéro 6 :</Label>
     
            <TextBox Grid.Column="2" Grid.Row="1" Name="tbNom" Height="26" Width="280" HorizontalAlignment="Left" VerticalAlignment="Center">
                <TextBox.Text>
                    <Binding Path="Nom" ElementName="joueur" UpdateSourceTrigger="PropertyChanged">
                    </Binding>
                </TextBox.Text>
            </TextBox>
     
            <StackPanel Grid.Column="2" Grid.Row="2" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum1" Valeur="1"></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="3" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum2" Valeur="1" ></my:NumericUpDown>  
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="4" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum3" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="5" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum4" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="6" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum5" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="7" Orientation="Horizontal">
                <my:NumericUpDown x:Name="NudNum6" Valeur="1" ></my:NumericUpDown>
            </StackPanel>
     
            <Grid Grid.Column="1" Grid.Row="8" Grid.ColumnSpan="2" >
     
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="150"/>
                </Grid.ColumnDefinitions>
     
                <Button Name="btAjouter" Grid.Column="1" Grid.Row="0" Content="Valider" Height="30" Width="150" Click="btAjouter_Click"></Button>
                <Button Name="btAnnuler" Grid.Column="2" Grid.Row="1" Content="Annuler" Height="30" Width="150" Click="btAnnuler_Click"></Button>
     
            </Grid>
     
        </Grid>
    </Window>

    mais ca ne fonctionne pas. pas d'erreur mais aucune valeur n'apparait dans ma textbox au clic du bouton.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Salut,
    il faut que ta classe Joueur implémente INotifyPropertyChanged. En VB, ça donne :
    Code vb : 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
    Namespace Verification_Lotto
     
        Public Class Joueur
            Implements INotifyPropertyChanged
     
            Private _Nom As String
            Public Property Nom() As String
                Get
                    Return _Nom
                End Get
                Set(ByVal value As String)
                    _Nom = value
                    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Nom"))
                End Set
            End Property
     
            Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
        End Class
     
    End Namespace
    (N'oublie pas de lever l'évènement PropertyChanged dans le Set de ta propriété)
    Dans le code behind de ta window, tu crées ensuite une Property MonJoueur par ex., de type Joueur. Sur le loaded, tu instancies un objet, et tu affectes le DataContext de ta window à cet objet :
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Class MainWindow
        Public Property MonJoueur As Verification_Lotto.Joueur
     
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
            Me.MonJoueur = New Verification_Lotto.Joueur
            Me.DataContext = MonJoueur
            Me.MonJoueur.Nom = "DUPONT"
        End Sub
    End Class
    Enfin, dans le Xaml de ta window, tu bindes ta textbox comme suit :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    <TextBox Text="{Binding Path=Nom}" />
    Et voilà !

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    C'est impeccable. Ca fonctionne parfaitement.

    Par contre j'ai un autre soucis, mais pour mes autres Controles (UserControl NumericUpDown)

    j'ai donc ma classe Joueur

    Code c# : 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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
     
    namespace Verification_Lotto
    {
        class Joueur : INotifyPropertyChanged
        {
            private string nom;
            private numeroLotto num1 = new numeroLotto();
            private numeroLotto num2 = new numeroLotto();
            private numeroLotto num3 = new numeroLotto();
            private numeroLotto num4 = new numeroLotto();
            private numeroLotto num5 = new numeroLotto();
            private numeroLotto num6 = new numeroLotto();
     
            public Joueur(string p_nom, int p_num1, int p_num2, int p_num3, int p_num4, int p_num5, int p_num6)
            {
                nom = p_nom;
                num1.Numero = p_num1;
                num2.Numero = p_num2;
                num3.Numero = p_num3;
                num4.Numero = p_num4;
                num5.Numero = p_num5;
                num6.Numero = p_num6;
            }
            public Joueur()
            {
                nom = "";
                num1.Numero = num2.Numero = num3.Numero = num4.Numero = num5.Numero = num6.Numero = 0;
     
            }
            public string Nom
            {
                set
                {
                    nom = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
                    }
                }
                get
                {
                    return nom;
                }
            }
            public numeroLotto Num1
            {
                set
                {
                    num1 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num1"));
                    }
                }
                get
                {
                    return num1;
                }
            }
            public numeroLotto Num2
            {
                set
                {
                    num2 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num2"));
                    }
                }
                get
                {
                    return num2;
                }
            }
            public numeroLotto Num3
            {
                set
                {
                    num3 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num3"));
                    }
                }
                get
                {
                    return num3;
                }
            }
            public numeroLotto Num4
            {
                set
                {
                    num4 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num4"));
                    }
                }
                get
                {
                    return num4;
                }
            }
            public numeroLotto Num5
            {
                set
                {
                    num5 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num5"));
                    }
                }
                get
                {
                    return num5;
                }
            }
            public numeroLotto Num6
            {
                set
                {
                    num6 = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Num6"));
                    }
                }
                get
                {
                    return num6;
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
        }
    }

    qui utilise des objet du type de ma classe numeroLotto

    Code c# : 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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace Verification_Lotto
    {
        class numeroLotto
        {
     
            int numero;
            bool tire;
     
            public numeroLotto(int p_numero, bool p_tire)
            {
                numero = p_numero;
                tire = p_tire;
            }
     
            public numeroLotto()
            {
                numero = 0;
                tire = false;
            }
     
            public int Numero
            {
                set
                {
                    numero = value;
                }
                get
                {
                    return numero;
                }
            }
            public bool Tire
            {
                set
                {
                    tire = value;
                }
                get
                {
                    return tire;
                }
            }
        }
    }

    et voici le binding que j'ai essayé de faire

    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    <my:NumericUpDown x:Name="NudNum1">
                    <my:NumericUpDown.Valeur>
                        <Binding Path="Num1" UpdateSourceTrigger="PropertyChanged"></Binding>
                    </my:NumericUpDown.Valeur>
                </my:NumericUpDown>

    Franchement, j'avais un doute que ca fonctionne.

    Comme puis-je lier la valeur de mon numericUpDown à la propriété Numero de mon object NumeroLotto?

    N'est ce pas plutôt dû à une mauvaise conception de ma part?

    Merci

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Est-ce que NumericUpDown est un userControl créé par tes soins ? De toute façon, sa propriété Valeur ne doit accepter que des Double et non des objets NumeroLotto...

    P.S : pourquoi crées-tu autant de propriétés NumeroLotto que de numéros à gérer ? Autant créer 1 seule propriété de type Collection, qui contiendra tous les numéros (ce qui te permettra, de plus, d'en gérer un nombre quelconque sans avoir à changer quoi que ce soit )

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    Oui c'est moi qui l'ai créé.
    Effectivement la propriété Valeur n'accepte qu'un int.

    c'est justement la mon problème car je veux lier Valeur à la propriété Numero de mon objet NumeroLotto qui est une propriété de mon joueur.

    Il est vrai qu'une collection aurait été bien, mais étant donné que j'ai déjà du mal avec le binding simple, je me vois mal faire du binding sur une collection

    est-ce que tu crois que je devrais laisser tomber ma classe NumeroLotto?
    en fait j'ai besoin d'un bool qui soit lié à mon numero.

    Merci

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    OK, donc le binding doit s'effectuer sur Num1.Numero (qui est aussi un int).
    Pour le reste, le problème n'est pas la classe NumeroLotto, mais toutes les propriétés Num1, Num2, etc. de Joueur. Crée plutôt une propriété Numeros sur Joueur :
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Public Class Joueur
     
        Public Property Numeros As ObservableCollection(Of NumeroLotto)
     
        End Property
     
    End Class

  8. #8
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    comme ceci alors?

    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace Verification_Lotto
    {
        class Joueur : INotifyPropertyChanged
        {
            private string nom;
            public ObservableCollection<numeroLotto> numeros = new ObservableCollection<numeroLotto>();
     
            public Joueur(string p_nom, int p_num1, int p_num2, int p_num3, int p_num4, int p_num5, int p_num6)
            {
                nom = p_nom;
                numeros.Add(new numeroLotto(p_num1, false));
                numeros.Add(new numeroLotto(p_num2, false));
                numeros.Add(new numeroLotto(p_num3, false));
                numeros.Add(new numeroLotto(p_num4, false));
                numeros.Add(new numeroLotto(p_num5, false));
                numeros.Add(new numeroLotto(p_num6, false));
            }
            public Joueur()
            {
                nom = "";
                numeros.Add(new numeroLotto());
                numeros.Add(new numeroLotto());
                numeros.Add(new numeroLotto());
                numeros.Add(new numeroLotto());
                numeros.Add(new numeroLotto());
                numeros.Add(new numeroLotto());
            }
            public string Nom
            {
                set
                {
                    nom = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
                    }
                }
                get
                {
                    return nom;
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
        }
    }
    Comment lier maintenant les éléments de ma collection à mes UserControl?

    Merci pour ton temps et tes réponses

  9. #9
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Tout à fait.
    Maintenant que ta propriété Numeros est une Collection, pour l'afficher, il te faut un contrôle qui affiche des ItemsCollection comme, par ex, une Listbox.
    Rajoute une listbox dans ton Xaml :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <ListBox ItemsSource="{Binding}" DisplayMemberPath="Numero" Name="ListBox1" />
    Dans le code behind, tu définis le DataContext de ta Listbox sur Joueur.Numeros :
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
            Me.MonJoueur = New Verification_Lotto.Joueur
            Me.DataContext = MonJoueur
            Me.MonJoueur.Nom = "DUPONT"
            'Pour tester :
            Dim n As New NumeroLotto
            n.Numero = 14
            Me.MonJoueur.Numeros.Add(n)
            Me.ListBox1.DataContext = MonJoueur.Numeros
    End Sub
    Et voilà !

    P.S : instancie ton ObservableCollection dans le constructeur de ta classe Joueur...

  10. #10
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    Il est donc impossible de pouvoir travailler avec mes numericUpDown?

    Merci

  11. #11
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Bien sûr que si ! Il te suffit de créer par ex un DataTemplate pour les Items de ta Listbox qui utilisera les NumericUpDown pour afficher tes numéros.

  12. #12
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut

    Je vais regarder ce que je peux trouver sur les DataTemplate.
    je suis vraiment débutant en WPF. Je n'ai donc jamais mis en oeuvre tout celà.
    En espérant que je m'en sors

  13. #13
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    C'est ultra-simple ! Tiens, un exemple vite fait où j'affiche la collection Numeros dans des TextBlocks précédés d'un label "Numéro :" :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <ListBox ItemsSource="{Binding}" Name="ListBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="Numéro : " Background="#FFDEF555" />
                            <TextBlock Text="{Binding Path=Numero}" Background="#FFDEF555" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
    </ListBox>
    Tu mets ce que tu veux pour afficher tes numéros : un TextBlock, un NumericiUpDown, etc. le principe est le même.

  14. #14
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    Impeccable. par contre j'ai encore des questions sur cette listBox.
    j'ai deux petits soucis d'affichage.

    1. Y a-t-il possibilité d'empêcher la sélection d'un item?
    2. Est-il possible d'empêcher un item 'blanc' en fin de liste?

    Merci

  15. #15
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    J'ai trouvé une solution à mes deux question
    --> ItemsControl

    Maintenant (normalement) une dernière question.
    Je dois tester si toutes mes NumericUpDown ont des valeurs différentes.

    j'ai deux possibilités :
    soit l'empêcher lors des sélections, c'est à dire lorsque l'on modifie les Valeurs des NumericUpDown, on vérifie que cette valeur ne se trouve pas déjà dans une des autre NumericUpDown, si elle s'y trouve déjà j'incrémente de 1 en plus.
    soit du ValidationRule, où les contrôles seraient fait lors du clic sur le bouton mais j'ai entendu dire que de la Validation sur un UserControl n'était pas facile, mais je trouve que c'est la façon la plus 'jolie'.

    Quelle serait la meilleure façon? à moins qu'il y en aurait une autre?

    Un grand Merci.

    c'est le plus gros morceau qu'il me reste à faire
    Le reste sera plus dans mes cordes.

  16. #16
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Pour empêcher 2 valeurs identiques dans 2 NumericUpDown, tu peux, au changement de valeur d'un NumericUpDown, lire les valeurs contenues dans les autres NumericUpDown (en parcourant la collection Children du StackPanel du DataTemplate (donne-lui un nom)) et en vérifiant si la valeur de celui qui a changé n'est pas égale à l'un des autres NumericUpDown...

  17. #17
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    désolé de t'embêter encore une fois, mais sur quel événement dois-je tester si ma Valeur a changé.
    l'évènement est défini dans la classe de mon UserControl, mais je ne peux l'utiliser à l'extérieur?

    Merci.

  18. #18
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    J'imagine que NumericUpDown est composé d'une TextBox ? Dans ton userControl, tu crées un RoutedEvent nommé, par ex, ValueChanged, que tu lèveras dans l'évènement TextChanged de la TextBox.
    Dans l'application hôte, tu t'abonnes à cet évènement et, dès qu'il se produit, tu compares la value du NumericUpDown qui a déclenché l'évènement aux Values de tous les autres...

  19. #19
    Membre régulier
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Points : 78
    Points
    78
    Par défaut
    J'ai réussi à créer mon RoutedEvent ( qu'est ce que tu m'en as appris des chose )

    mais voilà, je n'ai aucune information me permettant de dire quelle NumericUpDown a levé l'évènement. et il me semble ne pas pouvoir lui en donner une ( par exemple Name ) sur chacune des NumericUpDown.

    voici le code de mon itemControl qui affiche mes NumericUpDown

    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <ItemsControl ItemsSource="{Binding}" Name="listNud" Grid.Column="2" Grid.Row="2" Grid.RowSpan="6" HorizontalAlignment="Left" VerticalAlignment="Top">
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="FrameworkElement">
                        <Setter Property="Height" Value="30"></Setter>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <my:NumericUpDown x:Name="num" Valeur="{Binding Path=Numero}" Height="22" ValeurChanged="NumericUpDown_ValeurChanged" ></my:NumericUpDown>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    et voici le code behind

    Code c# : 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
     
    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.Shapes;
     
    namespace LottoVerification
    {
        /// <summary>
        /// Interaction logic for AjoutJoueur.xaml
        /// </summary>
        public partial class AjoutJoueur : Window
        {
     
            Joueur joueur;
     
            public AjoutJoueur()
            {
                InitializeComponent();
            }
     
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                joueur = new Joueur("", 1, 2, 3, 4, 5, 6);
                this.DataContext = joueur;
                this.listNud.DataContext = joueur.numeros;
            }
     
            private void btAjouter_Click(object sender, RoutedEventArgs e)
            {
                joueur.Nom = "C'est mon nom";
            }
     
            private void btAnnuler_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
     
            private void NumericUpDown_ValeurChanged(object sender, RoutedEventArgs e)
            {
     
            }
        }
    }

    est ce que je dois créer un nouvel objet NumericUpDown à partir de mon sender de mon évènement et le comparer à tous les items de mon ItemControl? ou il y a plus simple?

    Merci encore une fois pour ton aide.

  20. #20
    Membre actif
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Points : 265
    Points
    265
    Par défaut
    Je vois que dans ton code behind tu as juste rajouté une procédure NumericUpDown_ValeurChanged, ce qui ne liera en rien à l'évènement correspondant de ton NumricUpDown...
    Dans le constructeur de ta window :
    Code vb : Sélectionner tout - Visualiser dans une fenêtre à part
    AddHandler Me.Num.NumericUpDown_ValueChanged, AddressOf NumericUpDown_ValeurChanged
    (si l'évènement s'appelle NumericUpDown_ValueChanged dans ton userControl)

    Puis, dans la procédure NumericUpDown_ValeurChanged de ta window, tu récupères le NumericUpDown responsable de l'évènement grâce à e.Source.

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 3 123 DernièreDernière

Discussions similaires

  1. Création de formulaire par classe
    Par senacle dans le forum Général Python
    Réponses: 6
    Dernier message: 29/06/2018, 16h50
  2. Probleme de binding pour une classe herité de ModelVisual3D
    Par goast dans le forum Windows Presentation Foundation
    Réponses: 0
    Dernier message: 12/08/2009, 16h10
  3. formulaire bean classe ?!
    Par solarien dans le forum Débuter avec Java
    Réponses: 1
    Dernier message: 09/06/2008, 13h38
  4. Binding à partir de classes différentes
    Par pcayrol dans le forum Windows Presentation Foundation
    Réponses: 23
    Dernier message: 12/04/2008, 14h17
  5. Réponses: 6
    Dernier message: 19/04/2007, 15h03

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