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

Silverlight Discussion :

[Silverlight 2 beta 2]Problème de binding


Sujet :

Silverlight

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut [Silverlight 2 beta 2]Problème de binding
    J'ai un problème de binding que je n'avais pas avec la beta 1:

    J'ai créé une classe basique :
    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
     
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
     
    namespace SimpleBinding
    {
        public class Personnes
        {
            public string  Prenom { get; set; }
            public string  Nom { get; set; }
            public int Age { get; set; }
        }
    }
    ma page 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
     
    <UserControl x:Class="SimpleBinding.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
     
            <ListBox x:Name="lbPersonnes">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="Prenom"/>                   
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </UserControl>
    puis mon code-behind :

    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
     
    namespace SimpleBinding
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Page_Loaded);
            }
     
            void Page_Loaded(object sender, RoutedEventArgs e)
            {
                Personnes p = new Personnes
                {
                    Prenom = "Anton",
                    Nom = "Dvorak",
                    Age = 25
                };
                LayoutRoot.DataContext = p;
            }
        }
    }
    Lorsque je lance l'application j'ai une listebox vide alors que si je remplace ma listbox par une textbox ca fonctionne ???

    Merci de votre aide

  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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    je comprends pas trop pourquoi tu utilises une ListBox pour afficher quelque chose qui n'est pas une liste...

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    cet exemple est un exemple créé vite fait, ne trouvant pas comme afficher le résultat de ce que renvoyait un service WCF j'ai essayé avec un truc basique.
    Et le problème reste entier

  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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    essaie de faire comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
            void Page_Loaded(object sender, RoutedEventArgs e)
            {
                Personnes p = new Personnes
                {
                    Prenom = "Anton",
                    Nom = "Dvorak",
                    Age = 25
                };
                List<Personnes> lst = new List<Personnes>();
                lst.Add(p);
                LayoutRoot.DataContext = lst;
            }

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    J'ai bien essayé mais nada j'ai toujours une listbox vide ???

  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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    mets la liste dans la propriété ItemsSource de la ListBox, plutot que dans le DataContext

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Merci ca fonctionne, par contre toujours avec la listbox j'ai une liste vide dans cet exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BindingGrid.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <my:DataGrid x:Name="dgCustomers" Width="390" Height="100" VerticalAlignment="Top" ></my:DataGrid>
            <ListBox x:Name="listCustomers" Width="390" Height="100" VerticalAlignment="Bottom">
                <TextBlock Text="{Binding ContactName}"/>
            </ListBox>
        </Grid>
    </UserControl>
    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
     
    namespace BindingGrid
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Page_Loaded);
            }
     
            void Page_Loaded(object sender, RoutedEventArgs e)
            {
                ServiceReference1.Service1Client client = new BindingGrid.ServiceReference1.Service1Client();
     
                client.GetPersonnesCompleted += new EventHandler<BindingGrid.ServiceReference1.GetPersonnesCompletedEventArgs>(client_GetPersonnesCompleted);
                client.GetPersonnesAsync("p");
            }
     
            void client_GetPersonnesCompleted(object sender, BindingGrid.ServiceReference1.GetPersonnesCompletedEventArgs e)
            {
                dgCustomers.ItemsSource = e.Result;
                listCustomers.DataContext = e.Result; 
            }
        }
    }
    J'ai essayé :

    listCustomers.ItemsSource=e.Result mais j'ai cette erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    "Items collection must be empty before using ItemsSource."
    Mais ca avance merci

  8. #8
    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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    tu as pas mis des éléments manuellement dans la liste avant ? si c'est le cas il faut que tu la vides avant d'assigner ItemsSource

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    J'ai modofoé mon code XAML de la sorte :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BindingGrid.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <my:DataGrid x:Name="dgCustomers" Width="390" Height="100" VerticalAlignment="Top" ></my:DataGrid>
            <ListBox x:Name="listCustomers" Width="390" Height="100" VerticalAlignment="Bottom">
                <!-- <TextBlock Text="{Binding ContactName}"/> -->
            </ListBox>
        </Grid>
    </UserControl>
    Je n'ai plus d'erreur mais ma listbox est remplie uniquement de "BindingGrid.ServiceReference1.Customers" ?

  10. #10
    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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    Parce qu'il ne sait pas comment afficher un Customer
    Donc soit tu redéfinis la méthode ToString pour ta classe Customer, soit tu crées un ItemTemplate pour afficher les Customers

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Merci tout est résolu

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    jai crier gagné trop vite, voilà jai créé une classe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    namespace BindingGridWeb
    {
        [Serializable]
        public class Personnes
        {
            public string contactName { get; set; }
        }
    }
    le code de mon service :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
            public List<Personnes> GetName()
            {
                DataClasses1DataContext db=new DataClasses1DataContext();
                List<Personnes> liste = new List<Personnes>();
                var q = db.Customers.Where(c => c.City.StartsWith("p")).Select(c => new Personnes { contactName = c.ContactName });
                return q.ToList();
            }
    J'interroge Northwind

    mon code 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
     
    <UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BindingGrid.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        >
        <Grid x:Name="LayoutRoot" Background="White">
            <ListBox ItemsSource="{Binding}" Margin="5" x:Name="listPersonnes" Foreground="Blue" Background="Black">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding contactName}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </UserControl>
    puis mon code behind :

    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
     
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(Page_Loaded);
     
            }
     
            void Page_Loaded(object sender, RoutedEventArgs e)
            {
                ServiceReference1.Service1Client client = new BindingGrid.ServiceReference1.Service1Client();
     
                //client.GetPersonnesCompleted += new EventHandler<BindingGrid.ServiceReference1.GetPersonnesCompletedEventArgs>(client_GetPersonnesCompleted);
                //client.GetPersonnesAsync("p");
     
                client.GetNameCompleted += new EventHandler<BindingGrid.ServiceReference1.GetNameCompletedEventArgs>(client_GetNameCompleted);
                client.GetNameAsync();
            }
     
            void client_GetNameCompleted(object sender, BindingGrid.ServiceReference1.GetNameCompletedEventArgs e)
            {
                //dgCustomers.ItemsSource = e.Result;
                listPersonnes.ItemsSource = e.Result;
            }
    Résultat: encore unelistbox vide ???

    Par contre la grid elle se remplit bien avec le champ ContactName

  13. #13
    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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    C'est bizarre, parce que tu as mis en commentaire la ligne avec le datagrid, et tu as laissé celle avec la listbox... ça devrait faire le contraire

  14. #14
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    ce que voulez dire, c'est qu'en décommentant la ligne sur la datagrid, elle se remplit

  15. #15
    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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    Ah ok, je crois que je vois... dans ta listbox, tu mets ItemsSource="{Binding}", ce qui veut dire que tu bindes sur le datacontext. Mais comme tu n'assignes plus le résultat au DataContext, ça ne fonctionne pas... Essaie de virer simplement le binding sur ItemsSource, et de le faire seulement par le code

  16. #16
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    non ca ne fonctionne pas c'est toujours vide

  17. #17
    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 : 42
    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
    Points : 39 753
    Points
    39 753
    Par défaut
    ben là j'ai plus d'idées...

  18. #18
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    au cas où voici mon code, merci pour tout
    Fichiers attachés Fichiers attachés

  19. #19
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Points : 19 434
    Points
    19 434
    Par défaut
    Plusieurs questions:

    - Tu est sur que e.Result contient bien quelque chose ?
    - Ton service ServiceReference1, c'est un service WCF ? Car sur ta classe, il manque pas les attributs DataContract/DataMember ?

  20. #20
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2006
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2006
    Messages : 116
    Points : 63
    Points
    63
    Par défaut
    Citation Envoyé par Thomas Lebrun Voir le message
    Plusieurs questions:

    - Tu est sur que e.Result contient bien quelque chose ?
    - Ton service ServiceReference1, c'est un service WCF ? Car sur ta classe, il manque pas les attributs DataContract/DataMember ?

    oui e.Result contient ce que je veux (j'ai fait un debug ), quant au service oui je viens de le décorer des attributs et de mettre à jour la référence mais toujours pareil

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

Discussions similaires

  1. Problème de binding en silverlight 4
    Par Jdubernat dans le forum Silverlight
    Réponses: 2
    Dernier message: 17/02/2011, 09h25
  2. Probléme avec Bind
    Par rach20032 dans le forum Réseau
    Réponses: 2
    Dernier message: 06/07/2007, 11h36
  3. [Netbeans 6M9 JDk6.1 Matisse] problème de binding
    Par tralloc dans le forum NetBeans
    Réponses: 14
    Dernier message: 21/06/2007, 13h32
  4. problème de bindings avec DropDownList
    Par Vlatiska dans le forum ASP.NET
    Réponses: 14
    Dernier message: 17/03/2007, 14h04
  5. [C#][MySQL 5.x]Problème de Binding
    Par Oufti dans le forum Windows Forms
    Réponses: 2
    Dernier message: 07/05/2006, 23h44

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