Data context inside Control Template with Infragistics Grid

If  you are defining control template and inside that control template you need to access bindings, then it can get a bit tricky because the usual typical flow where DataContext just cascades through from the parent to child doesn’t apply here.

So for example, say you have infragistics xamdatagrid. In that you have an unbound field which has a CellValuePresenterStyle attribute set to a style in xaml in same file. Inside that style, you are setting the control template and inside control template you have a button. Not that button is eventually going to sit in that unbound field i.e. on every row. And you want to have an Id or something like that from every record and attached to the button as command parameter. In this case, the button can’t just use Id using {Binding Id} and set it in its command parameter. Nor it will be able to easily locate datacontext and set it command. For such situation, you would need to do something like this:

<Button  Width=”70″ Height=”35″ Command=”{Binding RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},Path=DataContext.DoWorkCommand}”  CommandParameter=”{Binding Path=Record.DataItem.Id, RelativeSource={RelativeSource TemplatedParent}}” HorizontalAlignment=”Center”>

You can see, here we are using RelativeSource to find the top ancestor i.e. the usercontrol and then grabbing its DataContext and then extracting its public property i.e. DoWorkCommand.
Secondly, for command paramter, we are using Record.DataItem.Id. Please note: Record.DataItem is infragistics built-in object and Id is our custom public property for each record.

This is how you access Command and CommandParamter inside the control template inside the style to be used by Infragistics datagrid.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s