Boujour a tous.
Je débute en WPF et je rencontre un problème avec du binding.
Si je créé une classe A et que dans le code de mon interface je créé une propriété de type ObservableCollection(Of A) puis que je la 'bind' à une ListBox tous se passe bien,lors d'un ajout la liste se met à jour.
Par contre, si je garde toujours ma classe A et que je créé une classe B avec une propriété de type ObservableCollection(Of A) et que dans le code de l'interface je declare un objet de de type B puis que je 'bind' la propriété de l'objet à la ListBox, cela ne marche pas.
La question est donc pourquoi je ne peut pas lier la propriété de mon objet?

Mon code est en VB mais j'accepte également le C#

Voici un peu de code pour imager mes dires

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
'****************************Classe A
 
Public Class A : Implements ComponentModel.INotifyPropertyChanged
    Private sNom As String
 
    Public Property Nom() As String
        Get
            Return sNom
        End Get
        Set(ByVal value As String)
            sNom = value
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("Nom"))
        End Set
    End Property
 
    Public Sub New(ByVal i As Integer)
        Me.Nom = "Nom" & i
    End Sub
 
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
 
'****************************Classe B
 
Public Class B : Implements ComponentModel.INotifyPropertyChanged
 
    Private lstc As ObjectModel.ObservableCollection(Of A)
    Public Property Liste() As ObjectModel.ObservableCollection(Of A)
        Get
            Return lstc
        End Get
        Set(ByVal value As ObjectModel.ObservableCollection(Of A))
            lstc = value
            RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("Liste"))
        End Set
    End Property
 
    Private i As Integer = 0
    Public Sub Ajout()
        If Me.lstc Is Nothing Then lstc = New ObjectModel.ObservableCollection(Of A)
        Dim t As New A(i)
        i += 1
        lstc.Add(t)
    End Sub
 
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
 
'*****************************Code de l'interface
 
Class Window1
    Private omonObjet As B
    Public ReadOnly Property MonObjet() As B
        Get
            Return omonObjet
        End Get
    End Property
 
    Public Sub New()
        omonObjet = New B()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        omonObjet.Ajout()
    End Sub
 
End Class
 
'****************************Code XAML
 
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Window1"
    Title="Window1" Height="396" Width="432" x:Name="Window1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:WpfApplication8="clr-namespace:WpfApplication8">
	<Window.Resources>
		<DataTemplate x:Key="DataTemplate1">
			<Canvas Width="168" Height="48">
				<Label Width="80" Height="32" Content="{Binding Path=Nom}" Canvas.Left="8" Canvas.Top="8" Background="#FFF13131"/>
			</Canvas>
		</DataTemplate>
	</Window.Resources>
    <Grid>
        <ListBox Margin="36,16,0,110" x:Name="ListBox1" Width="126" HorizontalAlignment="Left" ItemsSource="{Binding Path=MonObjet:Liste, ElementName=Window1}" ItemTemplate="{DynamicResource DataTemplate1}" />
        <Button Height="23" Margin="192,20,149,0" x:Name="Button1" VerticalAlignment="Top" Content="Button"/>
    </Grid>
</Window>
Merci par avance.