Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
515
How to get cell from DataContext for CellValuePresenter
posted

Hi,

i followed your post to write a custom cell presenter control:

  <igDP:XamDataGrid Name="DataGrid"  >
                <igDP:XamDataGrid.Resources>
                    <Style x:Key="CellStyle" TargetType="{x:Type igDP:CellValuePresenter}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">    
                                    <StackPanel Orientation="Horizontal">
                                        <!--<Label Content="{TemplateBinding DataContext}"  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"></Label>-->
                                        <my:CWpfIgZelleGui
                                            DataContext="{TemplateBinding DataContext}" 
                                            Width="{TemplateBinding Width}"
                                            Height="{TemplateBinding Height}"
                                            />
                                       
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </igDP:XamDataGrid.Resources>
            </igDP:XamDataGrid>

In the code i set the style and my cell control is displayed.

But the problem is with the datacontext:

The datacontext contains the DataRecord. How should i figure out, which cell to display?

 

 

 

 

 

 

Parents
No Data
Reply
  • 515
    Suggested Answer
    posted

    Ok, i made a workouround:

    [1] I added a DependencyProperty 'Field' to CWpfIgCellGui-control

    ' [2] I created a celltemplate in the codebehind for each Cell

    ' [3] in the template i assigned the field to the property

    [4] in CWpfIgCellGui i added another datacontext which i set to the cell which i can determine by evaluating [5] the datacontext for the control (which points to datarecord) and the field from my own dependency property.

    that is quite sophisticated to realize a standard usecase (and i guess displaying a cell value in a cellvaluepresenter should be a standard use case) so i hope there is a better solution. (The solution for my understanding would be that the data context contains the cell, not the record)

    here's the code:

    ' [1] I added a DependencyProperty 'Field' to CWpfIgCellGui-control

         Public Shared ReadOnly FieldProperty As DependencyProperty = DependencyProperty.Register("Field", GetType(Field), GetType(CWpfIgZelleGui), New PropertyMetadata(AddressOf FieldChanged))
        Public Property Field() As Field
            Get
                Return DirectCast(Me.GetValue(FieldProperty), Field)
            End Get
            Set(ByVal value As Field)
                Me.SetValue(FieldProperty, value)
            End Set
        End Property

    ' [2] I created a celltemplate in the codebehind for each Cell

            Dim aField As New UnboundField       
            Dim aCellStyle As New Style
            aCellStyle.TargetType = GetType(CellValuePresenter)
            Dim aTemplateSetter As New Setter
            aTemplateSetter.Property = CellValuePresenter.TemplateProperty
            Dim aTemplate As New ControlTemplate(GetType(CellValuePresenter))
            Dim aTemplateElementFactory As New FrameworkElementFactory(GetType(CWpfIgZelleGui))
            aTemplateElementFactory.SetValue(CWpfIgZelleGui.FieldProperty, aField) ' ' [3] in the template i assigned the field to the property 
            aTemplate.VisualTree = aTemplateElementFactory
            aTemplate.Seal()
            aTemplateSetter.Value = aTemplate
            aCellStyle.Setters.Add(aTemplateSetter)
            aField.Settings.CellValuePresenterStyle = aCellStyle
            aField.Name = Me.UnboundFieldName
            aField.DataType = GetType(Integer) 

     ' [4]

        Private Sub CWpfIgZelleGui_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
            Me.GridDataContextFüllen()
        End Sub
         Private Shared Sub FieldChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
            DirectCast(d, CWpfIgZelleGui).GridDataContextFüllen()
        End Sub

        Private Sub GridDataContextFüllen()
            Dim aField As Field = Me.Field
            Dim aRecord As DataRecord = DirectCast(Me.DataContext, DataRecord)
            Dim aCell As Cell
            If aRecord IsNot Nothing And aField IsNot Nothing Then
                aCell = aRecord.Cells(aField) ' [5] evaluating the cell which we need to display the cell value.
            Else
                aCell = Nothing
            End If
            Me.GridM.DataContext = aCell
        End Sub

     

Children