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 :

MVVM ListBox ItemSource Binding [Débutant]


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Août 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 7
    Par défaut MVVM ListBox ItemSource Binding
    Bonjour !

    Je me trouve devant un problème de rafraichissement de ma listbox alors que j'ai une ObservableCollection en ItemSource ; j'utilise le pattern MVVM.

    mon ViewModel :
    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
    Public Class MainWindowViewModel
        'implémente INotifyPropertyChanged & IDisposable
        Inherits ViewModelBase
     
        'Ma collection bindée
        Private _personList As ObservableCollection(Of Person)
        Public Property PersonList() As ObservableCollection(Of Person)
            Get
                If IsNothing(_personList) Then
                    _personList = GetPersonList()
                End If
                Return _personList
            End Get
            Set(ByVal value As ObservableCollection(Of Person))
                _personList = value
                OnPropertyChanged(Me, "PersonList")
            End Set
        End Property
     
        'constructeurs (vides)
        Sub New()
        End Sub
        Sub New(ByVal mainWindow As MainWindow)
        End Sub
     
        'génère une premier dataset
        Private Function GetPersonList() As ObservableCollection(Of Person)
            Dim l As New ObservableCollection(Of Person)
            l.Add(New Person("a", "b", "1", 1))
            l.Add(New Person("c", "d", "2", 2))
            l.Add(New Person("e", "f", "3", 3))
            l.Add(New Person("g", "h", "4", 4))
            l.Add(New Person("i", "j", "5", 5))
            Return l
        End Function
        'génère une deuxième dataset
        Private Function GetPersonList2() As ObservableCollection(Of Person)
            Dim l As New ObservableCollection(Of Person)
            l.Add(New Person("g", "h", "4", 4))
            l.Add(New Person("e", "f", "3", 3))
            l.Add(New Person("i", "j", "5", 5))
            l.Add(New Person("c", "d", "2", 2))
            l.Add(New Person("a", "b", "1", 1))
            Return l
        End Function
        'charge le deuxième dataset
        Public Sub Load()
            Me.PersonList = GetPersonList2()
        End Sub
        'supprime le premier élément de la liste
        Public Sub Delete()
            If Me.PersonList.Count > 0 Then
                Me.PersonList.RemoveAt(0)
                OnPropertyChanged(Me, "PersonList")
            End If
     
        End Sub
    End Class

    et voici mon xaml :
    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
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <Window.Resources>
            <local:MainWindowViewModel x:Key="MainWindowViewModelDataSource"/>
    	</Window.Resources>
     
        <Grid Name="stpnl" >
            <Grid.Resources>
                <DataTemplate x:Key="PersonTemplate" >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" MinWidth="50" Margin="5"/>
                        <TextBlock Text="{Binding Path=SubName}" MinWidth="50" Margin="5"/>
                        <TextBlock Text="{Binding Path=Age}" MinWidth="50" Margin="5"/>
                        <TextBlock Text="{Binding Path=ID}" MinWidth="50" Margin="5"/>
                    </StackPanel>
                </DataTemplate>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <ListBox Name="lbox"  Margin="5" Grid.Row="0"
             DataContext="{Binding Source={StaticResource MainWindowViewModelDataSource}}"		 
    		 ItemsSource="{Binding PersonList, Mode=TwoWay}"
             ItemTemplate="{StaticResource PersonTemplate}">            
            </ListBox>
     
            <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
                <Button Content="Load" Name="Button1" Width="150" Margin="20,0,20,0"/>
                <Button Content="Delete" Name="Button2" Width="150" Margin="20,0,20,0" />
            </StackPanel>

    L'appel du Load et Delete sont faits dans xaml.vb
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
            Dim d As MainWindowViewModel = Me.DataContext
            d.Load()
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
            Dim d As MainWindowViewModel = Me.DataContext
            d.Delete()
        End Sub

    Je ne comprend absolument pas ce qui cloche : la listbox est correctement remplie au démarrage, je passe bien dans les OnPropertyChanged, mais aucune modification de la View

    Si quelqu'un a une idée ??

    Merci !!

    Stéphane.

  2. #2
    Membre habitué
    Profil pro
    Inscrit en
    Août 2006
    Messages
    7
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 7
    Par défaut no brain
    Re,

    Après être reparti de zéro, comme c'est souvent utile dans ce genre de cas, j'ai trouvé le bug :
    Dans le xaml.vb, je chargeai le contexte de la window au lieu de celui de la listbox, donc je créai un nouveau ViewModel sur lequel je faisais les actions, et évidemment pas celui de la listbox qui logiquement ne bougeait pas !!
    donc suivant correct le code est :
    Code vb.net : Sélectionner tout - Visualiser dans une fenêtre à part
    Dim d As MainWindowViewModel = Me.lbox.DataContext

    -->out !!

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

Discussions similaires

  1. [Débutant] MVVM ListBox ItemSource Binding
    Par Trophonius dans le forum VB.NET
    Réponses: 1
    Dernier message: 07/08/2012, 17h23
  2. MVVM + Listbox + Multiselect : Bind sur selecteditems ?
    Par zax-tfh dans le forum Windows Presentation Foundation
    Réponses: 10
    Dernier message: 10/11/2010, 14h04
  3. [MVVM] Problème de binding, UserControl vers ViewModel
    Par Phil350 dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 06/10/2010, 17h35
  4. [SL 4] [RIA MVVM] Perte de binding dans les combobox
    Par stailer dans le forum Silverlight
    Réponses: 4
    Dernier message: 13/08/2010, 16h25
  5. Réponses: 12
    Dernier message: 22/04/2009, 11h17

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