2014-03-06 37 views
1

我的DataGrid中的XAML代碼:添加項目到列表中DataGrid不顯示

<DataGrid ItemsSource="{Binding Path=VoSamArtList}" Grid.Row="1" Margin="5,10,5,5" Grid.ColumnSpan="2"/> 

我的按鈕將項目添加到列表中的XAML:

<Button Grid.Row="2" Click="Button_Click"/> 

我在VB代碼屬性:

Private _oVoSamArtLijst As New List(Of Product) 
Public Property VoSamArtLijst As List(Of Product) 
    Get 
     Return _oVoSamArtLijst 
    End Get 
    Set(ByVal value As List(Of Product)) 
     _oVoSamArtLijst = value 
     RaisePropertyChanged() 
    End Set 
End Property 

我的按鈕單擊VB代碼:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs) 
    VoSamArtList.Add(New Product("Id", "Length", "Width", "Height", "Code", "Quality", "Description", "Price")) 
End Sub 

我在我的項目中有幾個其他Properties更新,就像他們必須從TextBoxesLabels一樣測試Binding。但將此ListDataGrid綁定似乎是一個困難的問題。如果我運行該程序,將生成列並使用Artikel Class具有正確的名稱。

我可能忘記了必須用BindingList Of's來完成的事情。當項目被添加或移除

回答

4

需要使用一個ObservableCollection

ObservableCollection

一個ObservableCollection通知。

,因爲我用C#

Private _oVoSamArtLijst As New ObservableCollection(Of Product) 
Public Property VoSamArtLijst As ObservableCollection(Of Product) 
    Get 
     Return _oVoSamArtLijst 
    End Get 
    Set(ByVal value As ObservableCollection(Of Product)) 
     _oVoSamArtLijst = value 
     RaisePropertyChanged() 
    End Set 
End Property 
+0

所以我現在有'公共VoSamArtLijst作爲新的ObservableCollection(產品)',並添加'RaisePropertyChanged( 「VoSamArtLijst」)'我'Button_Click'事件,這可能並不完美但沒有發生。 – Krowi

+0

你在哪裏有'公共VoSamArtLijst作爲新的ObservableCollection(Of Product)'? – Paparazzi

+0

這是我成爲一個noob ...我以爲我只需要'公共VoSamArtLijst作爲新的ObservableCollection(Of Product)',但不是。再次添加我的屬性,就像你在C#中的例子(我知道C#所以沒問題;))。像現在的魅力一樣工作,TNX。 – Krowi