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 :

Binding impossible sur une custom ListBox


Sujet :

Silverlight

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 35
    Points : 39
    Points
    39
    Par défaut Binding impossible sur une custom ListBox
    J'ai défini une listbox personnalisée de la mnière suivante:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <UserControl x:Class="TestBinding.CustomListBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ListBox x:Name="InnerList" />
    </UserControl>
    Bon, ca n'a pas trop d'intérêt présenté comme ca, mais c'est pour réduire le problème à son plus simple niveau.
    Dans le code-behind j'ai ceci:
    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
    using System.Windows.Controls;
    using System.Collections;
     
    namespace TestBinding
    {
        public partial class CustomListBox : UserControl
        {
            public CustomListBox()
            {
                InitializeComponent();
            }
     
            public IEnumerable ItemsSource
            {
                get { return InnerList.ItemsSource; }
                set { InnerList.ItemsSource = value; }
            }
        }
    }
    J'ai également créer une classe contenant mes données de cette manière:
    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
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Collections.Generic;
     
    namespace TestBinding
    {
        public class Employee
        {
            public string Nom { get; set; }
            public int Age { get; set; }
     
            public override string ToString()
            {
                return string.Format("{0} is {1} years old.", Nom, Age);
            }
        }
     
        public class EmployeesVM: INotifyPropertyChanged
        {
            public ObservableCollection<Employee> Employees { get; set; }
     
            public EmployeesVM()
            {
                Employees = new ObservableCollection<Employee>();
                Employees.Add(new Employee() { Nom = "Toto", Age = 27 });
                Employees.Add(new Employee() { Nom = "Tata", Age = 35 });
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
    Et dans la page qui utilise le usercontrol CustomListBox, j'ai ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <UserControl x:Class="TestBinding.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300" xmlns:o="clr-namespace:TestBinding">
        <UserControl.Resources>
            <o:EmployeesVM x:Key="MyViewModel" />
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource MyViewModel}">
            <StackPanel>
                <o:CustomListBox ItemsSource="{Binding Employees}" />
            </StackPanel>
        </Grid>
    </UserControl>
    Lorsque j'éxécute mon projet, j'obtiens cette exception sur le InitializeComponent de la page principale:
    AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 10 Position: 42]

    Je planche depuis quelque temps sur cette erreur, et je ne trouve pas de manière convenable pour faire mon binding sur mon custom contrôle.

    Merci

    ===================
    Blog : Silverlight News

  2. #2
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par tucod Voir le message
    AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 10 Position: 42]
    Ben l'erreur provient surement du code XAML alors tu dois regarder de ce côté. T'as du affecter quelque chose qui n'est correcte à un attribut.

  3. #3
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Tout à faire normal, pour faire du Binding tu peux pas passer par une propriété normale, il faut passer par un DependencyProperty

    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
    public partial class CustomListBox : UserControl
    {
        public CustomListBox()
        {
            InitializeComponent();
        }
     
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
     
        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CustomListBox), new PropertyMetadata(new PropertyChangedCallback(ItemsSourceChanged)));
     
        private static void ItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            CustomListBox list = sender as CustomListBox;
            list.InnerList.ItemsSource = e.NewValue as IEnumerable;
        }
    }

  4. #4
    Invité
    Invité(e)
    Par défaut
    Je croix que l'erreur provient du code suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <o:CustomListBox ItemsSource="{Binding Employees}" />
    La propriété ItemsSource n'est pas un Dependency Property. Si tu le transforme en Dependency Property ça devrait marcher. ;-)

  5. #5
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 35
    Points : 39
    Points
    39
    Par défaut
    Citation Envoyé par Skyounet Voir le message
    Tout à faire normal, pour faire du Binding tu peux pas passer par une propriété normale, il faut passer par un DependencyProperty

    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
    public partial class CustomListBox : UserControl
    {
        public CustomListBox()
        {
            InitializeComponent();
        }
     
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
     
        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CustomListBox), new PropertyMetadata(new PropertyChangedCallback(ItemsSourceChanged)));
     
        private static void ItemsSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            CustomListBox list = sender as CustomListBox;
            list.InnerList.ItemsSource = e.NewValue as IEnumerable;
        }
    }
    Merci beaucoup, c'était exactement ca mon problème

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

Discussions similaires

  1. Connexion impossible sur une machine cible
    Par Invité dans le forum Bases de données
    Réponses: 2
    Dernier message: 11/10/2009, 10h06
  2. [xubuntu] Ecriture impossible sur une clef USB
    Par Invité2 dans le forum Matériel
    Réponses: 2
    Dernier message: 07/11/2008, 19h07
  3. Filtre de binding source sur une trentaine de checkbox
    Par doudoustephane dans le forum Windows Forms
    Réponses: 12
    Dernier message: 31/05/2008, 00h31
  4. Drop impossible sur une base Mysql
    Par grouzou_08 dans le forum Requêtes
    Réponses: 3
    Dernier message: 14/08/2007, 11h42
  5. Connexion par TCP/IP impossible sur une deuxième instance
    Par sdelaunay dans le forum MS SQL Server
    Réponses: 6
    Dernier message: 25/08/2006, 22h24

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