OK... I have kindof a sepecialised case. I am getting log data back from a DB that has 5 static properties and then any number of variable properties. The class looks like this:
Prop 1, prop 2, prop 3, collection of properties 4-n... all possible properties are known.
Based on the type of log, properties 4-n will be different, again, all possible properties will be known.
The values of these properties will always be text.
binding properties 1-3 are easy... but is it possible to bind column values to items within a sub collection? for example, key="{Binding PropCollection.Prop4}"
Thanks in advance for any help
Marc
you are creating your columns like this
dg.Columns.Add(CreateXamTemplateColumn(i, "Hours" & i.ToString()))
the CreateXamTemplateColumn's second parameter takes the property name. You need to pass just the property name to the method and NOT concatenate it with the index. You are basically setting your binding expresion to
Binding Periods[0].Hours0
Brian Lagunas said: You are doing something wrong if you could not get data to show up. Check your output window in Visual Studio to see what errors are being thrown. Also, keep in mind that if you move to v10.1 you will have to use the UnboundColumn instead, because Infragistics now throws an exception if you try to do that method with a template column. Please provide your code that creates your template column and I will see if anything stands out at me.
You are doing something wrong if you could not get data to show up. Check your output window in Visual Studio to see what errors are being thrown. Also, keep in mind that if you move to v10.1 you will have to use the UnboundColumn instead, because Infragistics now throws an exception if you try to do that method with a template column. Please provide your code that creates your template column and I will see if anything stands out at me.
here is the test code for the GRID and IG Grid, they are on the same page
Private Sub DataGridDynamicCols_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
dataList = StaffMember.GetData()
dataGrid.AutoGenerateColumns = False dataGrid.ItemsSource = dataList dataGrid.Columns.Clear()
'Create first two dataGrid.Columns.Add(CreateTextColumn("Name", "Staff Name")) dataGrid.Columns.Add(CreateTextColumn("Department", "Company Department"))
Dim periodCount = dataList(0).Periods.Count
For i = 0 To periodCount
dataGrid.Columns.Add(CreateTemplateColumn(i, "Hours")) Next ''''''''''''''''' 'IG dg.AutoGenerateColumns = False dg.ItemsSource = dataList dg.Columns.Clear()
dg.Columns.Add(CreateXamTextColumn("Name", "Staff Name")) dg.Columns.Add(CreateXamTextColumn("Department", "Company Depart"))
'Dim periodCount = dataList(0).Periods.Count
Next
dg.InvalidateData()
End Sub
Private Shared Function CreateTextColumn(ByVal fieldName As String, _ ByVal title As String) As DataGridTextColumn
Dim column As New DataGridTextColumn() column.Header = title column.Binding = New System.Windows.Data.Binding(fieldName) Return column End Function
Private Function CreateColumnTemplate(ByVal index As Integer, _ ByVal propertyName As String) As String
Dim CellTemp As New StringBuilder() CellTemp.Append("<DataTemplate ") CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/") CellTemp.Append("2006/xaml/presentation' ") CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>") CellTemp.Append([String].Format("<TextBlock Text='{{Binding Periods[{0}].{1}}}' TextAlignment='Right'/>", _ index, propertyName)) CellTemp.Append("</DataTemplate>")
Return CellTemp.ToString() End Function
Private Function CreateColumnEditTemplate(ByVal index As Integer, _ ByVal propertyName As String) As String
Dim CellTemp As New StringBuilder() CellTemp.Append("<DataTemplate ") CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/") CellTemp.Append("2006/xaml/presentation' ") CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>") CellTemp.Append([String].Format("<TextBox Text='{{Binding Periods[{0}].{1}, Mode=TwoWay}}' TextAlignment='Right'/>", _ index, propertyName)) CellTemp.Append("</DataTemplate>")
Private Function CreateTemplateColumn(ByVal i As Integer, _ ByVal propName As String) As DataGridTemplateColumn
Dim column As New DataGridTemplateColumn()
column.Header = String.Format("Period#{0}.{1}", i, propName)
column.CellTemplate = XamlReader.Load(CreateColumnTemplate(i, propName)) '//display template column.CellEditingTemplate = XamlReader.Load(CreateColumnEditTemplate(i, propName)) ' //edit template Return column
End Function
Private Shared Function CreateXamTextColumn(ByVal fieldName As String, _ ByVal title As String) As Column
Dim column As New TextColumn()
column.HeaderText = title column.Key = fieldName Return column End Function
Private Function CreateXamTemplateColumn(ByVal i As Integer, _ ByVal propName As String) As ColumnBase
Dim column As New TemplateColumn()
column.Key = propName
column.HeaderText = String.Format("Period#{0}.{1}", i, propName)
column.ItemTemplate = XamlReader.Load(CreateColumnTemplate(i, propName))
column.EditorTemplate = XamlReader.Load(CreateColumnEditTemplate(i, propName)) ' //edit template Return column End Function
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim r As Object = dataList
End SubEnd Class
Public Class StaffMember
Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property Private _Department As String Public Property Department() As String Get Return _Department End Get Set(ByVal value As String) _Department = value End Set End Property Private _Periods As ObservableCollection(Of Period) Public Property Periods() As ObservableCollection(Of Period) Get Return _Periods End Get Set(ByVal value As ObservableCollection(Of Period)) _Periods = value End Set End Property
Public Shared Function GetData() As List(Of StaffMember)
Dim dataList As New List(Of StaffMember)() For i As Integer = 0 To 2 Dim member As New StaffMember() With _ {.Name = String.Format("Name#{0}", i), .Department = String.Format("Department#{0}", i)}
Dim periods As New ObservableCollection(Of Period)() For j As Integer = 0 To 4 periods.Add(New Period() With _ {.Title = String.Format("Period#{0}-{1}", i, j), .Hours = j})
Next member.Periods = periods dataList.Add(member) Next Return dataList End FunctionEnd Class
Public Class Period
Private _Title As String Public Property Title() As String Get Return _Title End Get Set(ByVal value As String) _Title = value End Set End Property Private _Hours As Integer Public Property Hours() As Integer Get Return _Hours End Get Set(ByVal value As Integer) _Hours = value End Set End PropertyEnd Class
Also, it apears you are using an interger as a key to your template column which is an invalid binding. You must use a string for your keys. You might need to generate a key such as
Key = string.format("Column{0}", y.Int1);
added doc with screen shots