This is a migrated thread and some comments may be shown as answers.

[Solved] How to bind Colum to specific collection item

1 Answer 153 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Broquet Mathieu
Top achievements
Rank 1
Broquet Mathieu asked on 20 Jan 2011, 03:17 PM
Hi All,

I try to bind a grid column to a specific item of an collection and used this item into my Cell Template.

Here is the item source of the grid:
mProducedAndPlannedGridData = New ObservableCollection(Of ProducedAndPlannedGridData)  
grdData.ItemsSource = mProducedAndPlannedGridData


Here is the ProducedAndPlannedGridData Class
Public Class ProducedAndPlannedGridData
    Private mProduct As String
    Private mDynCol As ObservableCollection(Of CumulProdData)
 
    Public Property Product() As String
        Get
            Return mProduct
        End Get
        Set(ByVal value As String)
            mProduct = value
        End Set
    End Property
 
    Public Property DynCol() As ObservableCollection(Of CumulProdData)
        Get
            Return mDynCol
        End Get
        Set(ByVal value As ObservableCollection(Of CumulProdData))
            mDynCol = value
        End Set
    End Property
End Class
 
Public Class CumulProdData
    Private mProduced As Double
    Private mPlanned As Double
 
    Public Property Produced() As Double
        Get
            Return mProduced
        End Get
        Set(ByVal value As Double)
            mProduced = value
        End Set
    End Property
 
    Public Property Planned() As Double
        Get
            Return mPlanned
        End Get
        Set(ByVal value As Double)
            mPlanned = value
        End Set
    End Property
End Class

Each item of ProducedAndPlannedGridData contain the same number of CumulProdData.

Here is the way i create and bind the colum
            AddColumnHeaders("Product", "Product", True, grdData)
 
            Dim int As Integer = 0
            If Not mProducedAndPlannedGridData Is Nothing Then
                For Each col In mProducedAndPlannedGridData(0).DynCol
                    AddColumnHeaders("DynCol[" + int.ToString + "]", col.ColName, True, grdData, , TryCast(Me.Resources("CellPlannedAndRealProductionTemplate"), DataTemplate), True)
                    int += 1
                Next
            End If
 
Private Sub AddColumnHeaders(ByVal strUniqueName As String, _
                             ByVal strColumnName As String, _
                             ByVal blnVisible As Boolean, _
                             ByVal grdIn As RadGridView, _
                             Optional ByVal strDataFormatString As String = "", _
                             Optional ByVal CellEditTemplate As DataTemplate = Nothing, _
                             Optional ByVal blnIsReadOnly As Boolean = True)
    Dim Column As New GridViewDataColumn
 
    Column.UniqueName = strUniqueName
    Column.Header = strColumnName
    Column.IsVisible = blnVisible
    Column.IsReadOnly = blnIsReadOnly
 
    If strDataFormatString <> "" Then
        Column.DataFormatString = strDataFormatString
    End If
 
    If Not CellEditTemplate Is Nothing Then
        Column.DataMemberBinding = New Binding(strUniqueName)
        Column.CellTemplate = CellEditTemplate
    End If
 
    grdIn.Columns.Add(Column)
End Sub

And here is the CellTemplate i used:
<DataTemplate x:Name="CellPlannedAndRealProductionTemplate">
    <StackPanel VerticalAlignment="Top" Orientation="Vertical">
        <TextBlock x:Name="txtProduced" Text="{Binding Produced}"/>
        <TextBlock x:Name="txtPlanned" Text="{Binding Planned}"/>
    </StackPanel>
</DataTemplate>

But the binding into the cellTemplate does not work.
Could you help me with this?

Thanks,
Mathieu

1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 25 Jan 2011, 05:36 PM
Hi Broquet Mathieu,

The problem is that the datacontext of the CellTemplate is the ProducedAndPlannedGridData, not the CumulProdData. You can check this blog post on how to

Creating a Silverlight DataContext Proxy to Simplify Data Binding in Nested Controls

Hope this helps.

All the best,
Veselin Vasilev
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Broquet Mathieu
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Share this question
or