Articles in this section
Category / Section

How to bind a underlying data object property inside the DataTemplate?

1 min read

You can bind the properties from underlying data object inside the template of DataBoundTemplate cell type. GridDataBoundTemplate cell sets GridStyleInfo as DataContext for template. You can pass data object via CellValue2 property in GridStyleInfo using QueryCellInfo event.

In the below code snippet, TextBlock inside template cell directly bound to property in underlying data object.

XAML

<syncfusion:GridTreeColumn HeaderText="First Name" MappingName="FirstName">
    <syncfusion:GridTreeColumn.StyleInfo>
        <syncfusion:GridStyleInfo CellType="DataBoundTemplate">
            <syncfusion:GridStyleInfo.CellItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Style.CellValue2.LastName}" />
                </DataTemplate>
            </syncfusion:GridStyleInfo.CellItemTemplate>
        </syncfusion:GridStyleInfo>
    </syncfusion:GridTreeColumn.StyleInfo>
</syncfusion:GridTreeColumn>

 C# 

void Model_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
    if (e.Style.CellType == "DataBoundTemplate")
    {
        var node = treeGrid.InternalGrid.GetNodeAtRowIndex(e.Style.RowIndex);
        if (node != null)
        {
            var item = node.Item as EmployeeInfo;
            e.Style.CellValue2 = item;
        }
    }
}

 

Sample Links:

WPF

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied