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


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Octobre 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Octobre 2010
    Messages : 19
    Par défaut Problème de binding
    Bonjour,
    Je développe une application en wpf;
    Voila la partie de code me posant problème :


    Presenter (ViexModel) :
    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
        public class ModifTourPresenter : BasePresenter
        {
            public ModifTourPresenter()
            {
                Title = "Gestion des tours";
                ListTourDb = BOLUnitOfWork.Tours.AllTours();
                ListTourAffiche = ListTourDb;
                listNom = new List<string>();
                foreach (var membre in BOLUnitOfWork.Membres.AllMember().OrderByDescending(m => m.TourCount))
                {
                    listNom.Add(membre.Nom);
                }
            }      
     
        }

    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
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
     
            <TextBlock Text="{Binding Title}" Grid.Row="0" Style="{StaticResource PageTitle}"></TextBlock>
            <ListView Grid.Row="1" ItemsSource="{Binding ListTourAffiche}" SelectedItem="{Binding SelectedTour, Mode=TwoWay}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="510" Height="150" Background="#83DFFF" x:Name="ListViewTour">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="80" Header="Date" DisplayMemberBinding="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
                        <GridViewColumn Width="100" Header="Nom" DisplayMemberBinding="{Binding Membre.Nom}"/>
                        <GridViewColumn Width="100" Header="Prénom" DisplayMemberBinding="{Binding Membre.Prenom}" />
                        <GridViewColumn Width="100" Header="Discipline" DisplayMemberBinding="{Binding Tarif.Nom}"/>
                        <GridViewColumn Width="80" Header="Durée" DisplayMemberBinding="{Binding Duree}"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Grid Margin="10, 30,10,10" Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
     
     
                <TextBlock Text="Date : "  Grid.Column="0" Grid.Row="0" Margin="3"/>
                <DatePicker x:Name="Date" Width="200" Grid.Column="1" Grid.Row="0" Margin="3" SelectedDate="{Binding SelectedTour.Date}"></DatePicker> 
     
                <TextBlock Text="Nom : " Grid.Column="0" Grid.Row="1"  Margin="3" />
                <ComboBox x:Name="Nom" ItemsSource="{Binding ListNom}" SelectedItem="{Binding SelectedTour.Membre.Nom}" Width="200" Grid.Column="1" Grid.Row="1" Margin="3" IsEditable="True"></ComboBox>
     
                <TextBlock Text="Prenom : " Grid.Column="0" Grid.Row="2" Margin="3" />
                <ComboBox x:Name="Prenom" SelectedItem="{Binding SelectedTour.Membre.Prenom}" Width="200" Grid.Column="1" Grid.Row="2" Margin="3" IsEditable="True"></ComboBox>
     
                <TextBlock Text="Discipline : " Grid.Column="0" Grid.Row="3" Margin="3" />
                <TextBox x:Name="Discipline" Width="200" Grid.Column="1" Grid.Row="3" Margin="3" Text="{Binding SelectedTour.Tarif.Nom}" />
     
                <TextBlock Text="Durée : " Grid.Column="0" Grid.Row="4" Margin="3"  />
                <TextBox x:Name="Durée" Width="200" Grid.Column="1" Grid.Row="4" Margin="3" Text="{Binding SelectedTour.Duree}" />
     
     
     
                <Button Content="Modifier !" Grid.Column="3" Height="80" Grid.Row="0" Grid.RowSpan="6" Margin="10,0,10,10" VerticalAlignment="Top" Style="{StaticResource MyButton}" />
                <Button Content="Supprimer !" Grid.Column="3"  Grid.Row="0" Grid.RowSpan="6" Margin="10, 90, 10, 0" Style="{StaticResource MyButton}" />
                <TextBlock TextAlignment="Center" x:Name="validat" Height="50" Text="" Foreground="Red" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" />
     
            </Grid>
            <Button Content="Retour menu" Style="{StaticResource MyButton}" FontSize="25" Grid.Row="3" Grid.Column="0" Margin="20" Grid.ColumnSpan="3" VerticalAlignment="Bottom" Height="80" Click="BackButtonClick" />
        </Grid>

    Mon problème :
    Quand on sélectionne un item de la listView, le Binding fonctionne bien et s'affiche dans les textBlock et comboBox en dessous.
    Par contre si je modifie le nom dans la comboBox ça me change toute les ligne de la listView qui avais le même nom...
    Par exemple si dans la listView j'ai deux personnes qui s'appelle Durant et que je clique sur un des deux pour le modifier, au moment ou je change le nom (Sur la comboBox), les deux Durant de la listView seront modifié ...

    Merci d'avance pour votre aide !
    Marc

  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
    Si les deux sont modifiés, ça veut sire que c'est le même objet (donc la même personne) pour les deux. C'est sensé être 2 personnes différentes?

  3. #3
    Membre averti
    Inscrit en
    Octobre 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Octobre 2010
    Messages : 19
    Par défaut
    Les Tour possèdent une relation avec Membre en base effectivement, je pensais que en récupérant le tour je récupérais une copie du membre...

    Comment résoudre ce problème ?
    J'ai fait une méthode quand on change le SelectedItem de la combox nom :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    (DataContext as ModifTourPresenter).SelectedTour.Membre = new Membre();
    Maintenant ça me change plus du tout le nom et prénom dans la listView.
    Y'a-t-il une autre solution (de préférence sans code behind) ?

    Merci d'avance !
    Marc

  4. #4
    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
    Ce que tu veux faire, c'est de changer la personne ? Par exemple Mr A est sur le tour 1 et 2, et tu veux assigner mr B sur le tour 2 ?

  5. #5
    Membre averti
    Inscrit en
    Octobre 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Octobre 2010
    Messages : 19
    Par défaut
    Oui et le problème c'est que je passe par deux comboBox (Nom et Prénom) du coup ça change le nom du membre et pas le membre du tour...
    Faudrait faire en sorte qu'au moment ou l'utilisateur change le nom dans la comboBox le Membre du tour soit mis a null jusqu’à ce que les comboBox Nom et Prénom soit remplie avec un Membre valide.

  6. #6
    Membre averti
    Inscrit en
    Octobre 2010
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Octobre 2010
    Messages : 19
    Par défaut
    J'ai trouvé une pseudo-solution, voila le code :

    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
    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
        public class ModifTourPresenter : BasePresenter
        {
            public ModifTourPresenter()
            {
                Title = "Gestion des tours";
                ListTourDb = BOLUnitOfWork.Tours.AllTours();
                ListTourAffiche = ListTourDb;
                listNom = new List<string>();
                ListPrenom = new List<string>();
                foreach (var membre in BOLUnitOfWork.Membres.AllMember().OrderByDescending(m => m.TourCount))
                {
                    listNom.Add(membre.Nom);
                }
            }
     
            #region Property + Getter & Setter
     
            public string Title { get; set; }
     
            private List<Tour> listTourDb;
     
            public List<Tour> ListTourDb
            {
                get { return listTourDb; }
                set { SetProperty(ref listTourDb, value); }
            }
     
            private List<Tour> listTourAffiche;
     
            public List<Tour> ListTourAffiche
            {
                get { return listTourAffiche; }
                set { SetProperty(ref listTourAffiche, value); }
            }
     
            private List<string> listNom;
     
            public List<string> ListNom
            {
                get { return listNom; }
                set { SetProperty(ref listNom, value); }
            }
     
            private List<string> listPrenom;
     
            public List<string> ListPrenom
            {
                get { return listPrenom; }
                set { SetProperty(ref listPrenom, value); }
            }
     
            private Tour selectedTour;
     
            public Tour SelectedTour
            {
                get { return selectedTour; }
                set
                {
                    SetProperty(ref selectedTour, value);
                    SetProperty(ref selectedLastName, (value as Tour).Membre.Nom);
                    OnPropertyChanged("SelectedLastName");
                    SetProperty(ref selectedFirstName, (value as Tour).Membre.Prenom);
                    OnPropertyChanged("SelectedFirstName");
                    SetProperty(ref listPrenom,
                               BOLUnitOfWork.Membres.AllMember()
                                            .Where(m => m.Nom == (value as Tour).Membre.Nom)
                                            .OrderByDescending(m => m.TourCount)
                                            .Select(m => m.Prenom)
                                            .ToList());
                    OnPropertyChanged("ListPrenom");
                }
            }
     
            private string selectedLastName;
     
            public string SelectedLastName
            {
                get { return selectedLastName; }
                set 
                { 
                    SetProperty(ref selectedLastName, value);
                    SetProperty(ref listPrenom,
                                BOLUnitOfWork.Membres.AllMember()
                                             .Where(m => m.Nom == value)
                                             .OrderByDescending(m => m.TourCount)
                                             .Select(m => m.Prenom)
                                             .ToList());
                    OnPropertyChanged("ListPrenom");
                }
            }
     
            private string selectedFirstName;
     
            public string SelectedFirstName
            {
                get { return selectedFirstName; }
                set
                {
                    SetProperty(ref selectedFirstName, value);
                    if (value != null)
                    {
                        SelectedTour.MembreID = BOLUnitOfWork.Membres.GetMemberByName(value, selectedLastName).ID;
                        SelectedTour.Membre = BOLUnitOfWork.Membres.GetMemberByName(value, selectedLastName);
     
                        OnPropertyChanged("SelectedTour");
                        OnPropertyChanged("ListTourAffiche");
                    }
     
                }
            }
     
            #endregion
     
     
     
        }

    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
     <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
     
            <TextBlock Text="{Binding Title}" Grid.Row="0" Style="{StaticResource PageTitle}"></TextBlock>
            <ListView Grid.Row="1" ItemsSource="{Binding ListTourAffiche, Mode=TwoWay}" SelectedItem="{Binding SelectedTour, Mode=TwoWay}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="510" Height="150" Background="#83DFFF"  x:Name="ListViewTour">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="80" Header="Date" DisplayMemberBinding="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
                        <GridViewColumn Width="100" Header="Nom" DisplayMemberBinding="{Binding Membre.Nom}"/>
                        <GridViewColumn Width="100" Header="Prénom" DisplayMemberBinding="{Binding Membre.Prenom}" />
                        <GridViewColumn Width="100" Header="Discipline" DisplayMemberBinding="{Binding Tarif.Nom}"/>
                        <GridViewColumn Width="80" Header="Durée" DisplayMemberBinding="{Binding Duree}"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Grid Margin="10, 30,10,10" Grid.Row="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
     
     
                <TextBlock Text="Date : "  Grid.Column="0" Grid.Row="0" Margin="3"/>
                <DatePicker x:Name="Date" Width="200" Grid.Column="1" Grid.Row="0" Margin="3" SelectedDate="{Binding SelectedTour.Date}"></DatePicker> 
     
                <TextBlock Text="Nom : " Grid.Column="0" Grid.Row="1"  Margin="3" />
                <ComboBox x:Name="Nom" ItemsSource="{Binding ListNom}"  Text="{Binding SelectedLastName, Mode=TwoWay}" Width="200" Grid.Column="1" Grid.Row="1" Margin="3" IsEditable="True"></ComboBox>
     
                <TextBlock Text="Prenom : " Grid.Column="0" Grid.Row="2" Margin="3" />
                <ComboBox x:Name="Prenom" ItemsSource="{Binding ListPrenom}" SelectedItem="{Binding SelectedFirstName, Mode=TwoWay}" Width="200" Grid.Column="1" Grid.Row="2" Margin="3" IsEditable="True"></ComboBox>
     
                <TextBlock Text="Discipline : " Grid.Column="0" Grid.Row="3" Margin="3" />
                <TextBox x:Name="Discipline" Width="200" Grid.Column="1" Grid.Row="3" Margin="3" Text="{Binding SelectedTour.Tarif.Nom}" />
     
                <TextBlock Text="Durée : " Grid.Column="0" Grid.Row="4" Margin="3"  />
                <TextBox x:Name="Durée" Width="200" Grid.Column="1" Grid.Row="4" Margin="3" Text="{Binding SelectedTour.Duree}" />
     
     
     
                <Button Content="Modifier !" Grid.Column="3" Height="80" Grid.Row="0" Grid.RowSpan="6" Margin="10,0,10,10" VerticalAlignment="Top" Style="{StaticResource MyButton}" />
                <Button Content="Supprimer !" Grid.Column="3"  Grid.Row="0" Grid.RowSpan="6" Margin="10, 90, 10, 0" Style="{StaticResource MyButton}" />
                <TextBlock TextAlignment="Center" x:Name="validat" Height="50" Text="" Foreground="Red" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="3" />
     
            </Grid>
            <Button Content="Retour menu" Style="{StaticResource MyButton}" FontSize="25" Grid.Row="3" Grid.Column="0" Margin="20" Grid.ColumnSpan="3" VerticalAlignment="Bottom" Height="80" Click="BackButtonClick" />
        </Grid>

    Par contre je comprend pas pourquoi mais la listView ne se met pas à jour quand je sélectionne un prénom ?
    En utilisant un point d’arrêt dans le setter du SelectedFirstName je vois que le code fonctionne bien et que le SelectedTour et le ListTourAffiche sont bien mis a jour ...

    Merci d'avance !
    Marc

Discussions similaires

  1. [WPF] Probléme de binding dans un UserControl :(
    Par UNi[FR] dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 17/07/2008, 16h51
  2. Probléme avec Bind
    Par rach20032 dans le forum Réseau
    Réponses: 2
    Dernier message: 06/07/2007, 12h36
  3. [Netbeans 6M9 JDk6.1 Matisse] problème de binding
    Par tralloc dans le forum NetBeans
    Réponses: 14
    Dernier message: 21/06/2007, 14h32
  4. problème de bindings avec DropDownList
    Par Vlatiska dans le forum ASP.NET
    Réponses: 14
    Dernier message: 17/03/2007, 15h04
  5. [C#][MySQL 5.x]Problème de Binding
    Par Oufti dans le forum Windows Forms
    Réponses: 2
    Dernier message: 08/05/2006, 00h44

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