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 listview Collection d'objets


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    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
    Par défaut Binding listview Collection d'objets
    Bonjour,

    j'ai toujours quelques soucis avec le binding.

    voilà, j'ai

    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
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
     
    namespace LottoVerification
    {
        class NumeroLotto : INotifyPropertyChanged
        {
            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;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Numero"));
                    }
                }
                get
                {
                    return numero;
                }
            }
            public bool Tire
            {
                set
                {
                    tire = value;
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Tire"));
                    }
                }
                get
                {
                    return tire;
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
     
        }
    }
    une 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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
     
    namespace LottoVerification
    {
        class Joueur : INotifyPropertyChanged
        {
            private bool continuer;
            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;
                    continuer = true;
     
                    if (String.IsNullOrEmpty(value))
                    {
                        continuer = false;
                        throw new ApplicationException();
                    }
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
                    }
                }
                get
                {
                    return nom;
                }
            }
     
            public bool Continuer
            {
                get
                {
                    return continuer;
                }
                set
                {
                    continuer = value;
                }
            }
     
            public void trierNumeros()
            {
                NumeroLotto numTmp = new NumeroLotto();
     
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        if (i != j)
                        {
                            if (this.numeros[i].Numero < this.numeros[j].Numero)
                            {
                                numTmp = this.numeros[i];
                                this.numeros[i] = this.numeros[j];
                                this.numeros[j] = numTmp;
                            }
                        }
                    }
                }
     
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }

    et ma fenêtre principale

    XAML

    Code xml : 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
     
    <Window x:Class="LottoVerification.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="600" Width="800" Loaded="Window_Loaded">
     
        <Window.Resources>
            <ResourceDictionary>
                <Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
                <Style x:Key="RowStyle" TargetType="ListViewItem">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListViewItem">
                                        <Border BorderBrush="Green"
                                                BorderThickness="1"
                                                CornerRadius="20"
                                                Height="40"
                                                Width="500"
                                                Background="LightGreen"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Margin="3">
                                            <StackPanel Orientation="Horizontal"
                                                        HorizontalAlignment="Center"
                                                        VerticalAlignment="Center">
                                                <GridViewRowPresenter Content="{TemplateBinding Content}"
                                                                      Columns="{TemplateBinding GridView.ColumnCollection}"/>
                                            </StackPanel>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="false">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListViewItem">
                                        <Border BorderBrush="Red"
                                                BorderThickness="1"
                                                CornerRadius="20"
                                                Height="40"
                                                Width="500"
                                                Background="Pink"
                                                HorizontalAlignment="Center"
                                                VerticalAlignment="Center"
                                                Margin="3">
                                            <StackPanel Orientation="Horizontal"
                                                        HorizontalAlignment="Center"
                                                        VerticalAlignment="Center">
                                                <GridViewRowPresenter Content="{TemplateBinding Content}"
                                                                      Columns="{TemplateBinding GridView.ColumnCollection}"/>
                                            </StackPanel>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ResourceDictionary>
        </Window.Resources>
     
        <Grid>
     
            <Grid.RowDefinitions>
                <RowDefinition Height="25"/>
                <RowDefinition/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
     
            <Menu Grid.Row="0">
                <MenuItem Header="Joueurs">
                    <MenuItem Click="MenuItem_Click" Header="Ajouter"/>
                </MenuItem>
            </Menu>
     
            <ListView Grid.Row="1" Name="listJoueurs" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource RowStyle}">
                <ListView.View>
                    <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock>
                                        <TextBlock.Text>
                                            <Binding Path="Nom" UpdateSourceTrigger="PropertyChanged"></Binding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ItemsControl ItemsSource="{Binding Path=numeros}" DataContext="joueurs">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=Numero}"></TextBlock>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                    </ItemsControl>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
     
            <Button Content="Supprimer joueurs" Click="Button_Click" Grid.Row="2"></Button>
     
        </Grid>
    </Window>

    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
     
    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;
    using System.Collections.ObjectModel;
     
    namespace LottoVerification
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            XmlReader readerXml = new XmlReader();
     
            public Window1()
            {
                InitializeComponent();
            }
     
            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                AjoutJoueur a = new AjoutJoueur();
                a.ShowDialog();
            }
     
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                XmlReader reader = new XmlReader();
                reader.DeleteAllJoueurs();
            }
     
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                ObservableCollection<Joueur> joueurs;
                joueurs = readerXml.GetAllJoueurs();
                listJoueurs.DataContext = joueurs;
            }
        }
    }

    Le but de ma fenêtre principale est d'afficher une collection d'objets Joueur, collection récupérée dans un fichier XML.

    Je veux afficher ces objets via une listview.

    J'arrive bien à afficher la propriété Nom de mes joueurs.
    mais je n'arrive pas à afficher ma collection de NumeroLotto qui appartient à mon joueur.

    il ne me met pas d'erreur, mais il ne m'affiche rien.

    Quelqu'un aurait une idée.

    Un grand merci.

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Enlève ça :
    En faisant ça, tu affectes la chaine "joueurs" au DataContext... et une chaine n'a pas de propriétés "numeros"
    Le DataContext est déjà un joueur, tu n'as rien à faire pour l'affecter

  3. #3
    Membre confirmé
    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
    Par défaut
    ca ne change rien.
    Ils ne s'affichent toujours pas.

    une autre idée?

    Merci

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Ah ok, je crois que je vois... dans ta classe, numeros est un champ, et on ne peut binder que sur une propriété. De toutes façons il est déconseillé de créer des champs publics, sauf dans cas très spécifiques...

    Modifie ta classe Joueur comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ...
     
    private readonly ObservableCollection<NumeroLotto> _numeros = new ObservableCollection<NumeroLotto>();
     
    public ObservableCollection<NumeroLotto> Numeros
    {
        get { return _numeros; }
    }
     
    ...
    (et ma réponse précédente est toujours valable)

  5. #5
    Membre confirmé
    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
    Par défaut
    Impeccable ca fonctionne.

    Merci.

    maintenant j'ai une autre petite question.
    Est-il possible de faire afficher les items d'une itemsControl à l'horizontal?
    au lieu de la verticale?

    Merci

  6. #6
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    oui, il faut modifier le ItemsPanel :

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <ItemsControl ItemsSource="..." ...>
        <ItemsControL.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        ...
    </ItemsControl>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Binding ComboBox sur collection d'objet
    Par NejNej dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 04/12/2009, 16h42
  2. Binding listView et objet d'une classe dans une autre classe
    Par warmy dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 09/06/2009, 14h26
  3. [Struts] <logic:iterate> sur une collection d objets c
    Par trax020 dans le forum Struts 1
    Réponses: 2
    Dernier message: 12/05/2005, 00h11
  4. Probléme collection d'objets
    Par Contrec dans le forum MFC
    Réponses: 1
    Dernier message: 14/04/2005, 10h08
  5. [VB6] Sauvegarder une collection d'objets
    Par Sayagh dans le forum VB 6 et antérieur
    Réponses: 7
    Dernier message: 19/09/2003, 11h58

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