wpf datagrid binding to collection of objects

Aug 18, 2009 at 10:31 AM


I am trying to bind data of a collection of objects to one datagrid.

Each of the objects have a propertyObject with properties.

The propertyObject:

Imports System.ComponentModel

Public Class propertyObject
  Implements INotifyPropertyChanged

  Private _name As String
  Public ReadOnly Property name() As String
      Return _name
    End Get
  End Property

  Private _value As Double
  Public Property value() As Double
      Return _value
    End Get
    Set(ByVal value As Double)
      _value = value
    End Set
  End Property

  Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

  Protected Sub OnPropertyChanged(ByVal propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
  End Sub
End Class

A object with properties:

Public Class objectWithProperties
  Private myPropertyList As List(Of propertyObject)

  Public Sub New()

    myPropertyList.Add(New propertyObject("A", 10.1))
    myPropertyList.Add(New propertyObject("B", 11.1))
    myPropertyList.Add(New propertyObject("C", 12.1))
    myPropertyList.Add(New propertyObject("D", 13.1))
    myPropertyList.Add(New propertyObject("E", 14.1))
    myPropertyList.Add(New propertyObject("F", 15.1))

  End Sub

End Class

I only know at runtim how many objectWithProperties I have.

Now I would like to display all values with the same name in one ROW of a datagrid.


| ValueName | Value Object1 | Value Object2 |

|           A             |           10.1           |           10.2          |

|           B             |           11.1           |           11.2          |


I thought about collection data at runtime and bind the collection to the datagrid. With use of a dataset I can display a collection values, but changing data at the datagrid does not change my propertyObject.

I tried to don't use the propertyObject and implement the properties directly in my objectWithProperties, but the problem is the same. I have to display the values with the same name of each object in one row.

Did anyone see a solution for this case?




Aug 18, 2009 at 2:19 PM

I am not sure if my question is understandable.

I can create for each object a new datagrid and bind the object properties to the datagrid. That works fine.

Unfortunately I need to have only one datagrid.

The question is,

  1. how could I merge the datagrids to one datagrid, or
  2. how can I add for each additional object one column to the existing datagrid?

Any suggestions?