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.
Partager