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 :

Concatener 2 champs dans une combobox à partir d'un DataContext [Débutant]


Sujet :

Windows Presentation Foundation

  1. #1
    Membre habitué Avatar de jubourbon
    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 540
    Points : 189
    Points
    189
    Par défaut Concatener 2 champs dans une combobox à partir d'un DataContext
    Bonjour à tous,

    J’ai un programme avec une combobox dans laquelle je veux afficher tous les noms et prénoms des personnes presnetes dans une table Personne.
    Ma table personne à la structure suivante: ID, Nom, Prenom.

    J'ai créé pour accéder aux donner une classe linqtosql.

    Voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    DCDataContext dc = new DCDataContext();
     
                comboBox1.ItemsSource = dc.Personne;
                comboBox1.DisplayMemberPath = "Nom + ' ' + Prenom";
                comboBox1.SelectedValuePath = "Id";
    Mais au final ma combobox reste blanche.

    Si quelqu'un peux me dire comment faire cela.

    Merci d'avance.

  2. #2
    Modérateur
    Avatar de DotNetMatt
    Homme Profil pro
    CTO
    Inscrit en
    Février 2010
    Messages
    3 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : CTO
    Secteur : Finance

    Informations forums :
    Inscription : Février 2010
    Messages : 3 611
    Points : 9 742
    Points
    9 742
    Billets dans le blog
    3
    Par défaut
    Ce n'est pas possible de faire ça via DisplayMemberPath. Il y a deux solutions :

    - Utiliser un DataTemplate :
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} {1}">
                    <Binding Path="Nom"/>
                    <Binding Path="Prenom"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
    - Rajouter une propriété dans ta classe source (Personne) :
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public string NomComplet
    {
        get { return string.Format("{0} {1}", this.Nom, this.Prenom); }
    }

  3. #3
    Membre habitué Avatar de jubourbon
    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 540
    Points : 189
    Points
    189
    Par défaut
    Ta solution me parait bien sympathique .

    Citation Envoyé par DotNetMatt Voir le message
    - Utiliser un DataTemplate :
    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} {1}">
                    <Binding Path="Nom"/>
                    <Binding Path="Prenom"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
    Mais je ne sais pas la mettre en place.

    Voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     <ComboBox Height="23" HorizontalAlignment="Left" Margin="108,118,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DataContext="{Binding}">
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1}">
                                <Binding Path="Nom"/>
                                <Binding Path="Prenom"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ComboBox>
    et dans le constructeur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboBox1.DataContext = dc.Tintervenants;
    Mais la combobox m'affiche "System.Windows.DataTemplate"

  4. #4
    Modérateur
    Avatar de DotNetMatt
    Homme Profil pro
    CTO
    Inscrit en
    Février 2010
    Messages
    3 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : CTO
    Secteur : Finance

    Informations forums :
    Inscription : Février 2010
    Messages : 3 611
    Points : 9 742
    Points
    9 742
    Billets dans le blog
    3
    Par défaut
    Tu y es presque, il te manque la propriété <ComboBox.ItemTemplate> entre <ComboBox ...> et <DataTemplate>

    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
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="108,118,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DataContext="{Binding}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}{0} {1}">
                            <Binding Path="Nom"/>
                            <Binding Path="Prenom"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

  5. #5
    Membre habitué Avatar de jubourbon
    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 540
    Points : 189
    Points
    189
    Par défaut
    J'ai bien suivi ta dernière syntaxe, mais pas mieux, maintenant j'ai une combobox complétement vide.

    Je vois pas trop d’où vient le probleme

  6. #6
    Modérateur
    Avatar de DotNetMatt
    Homme Profil pro
    CTO
    Inscrit en
    Février 2010
    Messages
    3 611
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : CTO
    Secteur : Finance

    Informations forums :
    Inscription : Février 2010
    Messages : 3 611
    Points : 9 742
    Points
    9 742
    Billets dans le blog
    3
    Par défaut
    Par rapport au bloc de code que tu as publié ici, dans ton premier post, as-tu bien supprimé la ligne n°4 dans ton appli ?

  7. #7
    Membre habitué Avatar de jubourbon
    Homme Profil pro
    Inscrit en
    Octobre 2008
    Messages
    540
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 540
    Points : 189
    Points
    189
    Par défaut
    J'avais bien supprimé la ligne mais j'ai du m'embrouillé dans mes copiés collés, il manquait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <Binding Path="Nom"/>
    <Binding Path="Prenom"/>
    Ça fonctionne tout de suite mieux

    Merci pour ton aide.

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

Discussions similaires

  1. Concatener 2 champs dans une liste déroulante
    Par RudyWI dans le forum Requêtes et SQL.
    Réponses: 3
    Dernier message: 15/05/2008, 11h33
  2. Réponses: 11
    Dernier message: 08/01/2008, 11h36
  3. Réponses: 5
    Dernier message: 28/12/2006, 15h41
  4. concatener 2 champs dans une requete
    Par jojo57 dans le forum Access
    Réponses: 4
    Dernier message: 17/05/2006, 15h29
  5. concatener 2 champs dans une table
    Par energies dans le forum Access
    Réponses: 2
    Dernier message: 14/04/2006, 14h24

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