Nasenbaer
Nasenbaer

Reputation: 4900

DataGrid is not updating rows from List(of ) object

I already had this stuff working in another project but now it does not. I have no more idea why.

  1. Set DataGrid ItemsSource to {Binding}
  2. Create new List(of myclass)
  3. Create public list Dim MyList As New List(of myclass)
  4. Bind DataGrid.DataContext = MyList when Instance is created
  5. Set DataGrid Column definitions to some Items on the myclass properties

I added my first class on the list MyList.Add(New myclass()) now, this item is visible. but after creating the instance and after the datagrid is visible, the rows do not update. MyList.Add(New myclass()) and no second row is visible.

Only when doing MyDataGrid1.Items.Refresh the items are updated correctly. But the List(of ) class already included the INotifyPropertyChanged interface as far as I understand.

Any ideas why it fails?

here some additional code

        <DataGrid.Columns>
            <DataGridTextColumn         Header="N°"                 Binding="{Binding Path=LineNumber, StringFormat='{}{0:000}'}"           Width="SizeToCells"     MinWidth="30" IsReadOnly="True"  />

            <DataGridTextColumn         Header="HEX"                Binding="{Binding HexText}"         Width="SizeToHeader"    MinWidth="80" IsReadOnly="True"  FontWeight="Bold"                    />
            <DataGridTextColumn         Header="TextNormalized"     Binding="{Binding Description}"     Width="*"     MinWidth="80" IsReadOnly="True"  FontSize="12"       />
        </DataGrid.Columns>
    </DataGrid>

Some codebehind:

 Private ListData As New List(Of clsHexRowData)

Instance:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    Me.dgv.ItemsSource = Me.ListData
    Me.ListData.Clear()
    Dim item As New clsQueueItem
    item.FromString("Hallo")
    Me.AddItem(item)
End Sub

this is visible, but when adding new rows after initializing the page, the items are not updated and a dgv.Items.Refresh is needed which I don't understand why.

Upvotes: 0

Views: 327

Answers (1)

blindmeis
blindmeis

Reputation: 22435

use ObservableCollection(of myclass) instead of list. you will see the changes when you add or remove items.

EDIT

 //add
 Application.Current.Dispatcher.BeginInvoke((Action)(()=> { Me.ListData.Add(new Item());}))

Upvotes: 1

Related Questions