Infragistics(R) NetAdvantage(R) for WPF
Using Custom Cell Renderers


Glossary Item Box

With xamDataGrid™, xamDataCarousel™, and xamDataPresenter™ you can change the control displayed in a Field. As shown in Specify a ValueEditor for a Field in xamDataGrid, it isn't difficult to switch the editor that a Field is using. What if you don't want an editor in a Field, but perhaps a Button or some other type of content presenter control. With Microsoft® Windows® Presentation Foundation this type of modification isn't complex at all. By making a copy of the CellValuePresenter's Style and changing the ControlTemplate, you can quickly modify what content presenter is used for a Field.

The following procedure assumes you have a data bound xamDataGrid. For more information, see Creating xamDataGrid in XAML. The following steps work for the xamDataCarousel or xamDataPresenter.

Follow these steps to override a Field's CellValuePresenter and use a Button as a content presenter.

  1. You need to define a custom Style in the Resources section. The following XAML defines a Style for the CellValuePresenter object. The XAML overrides the ControlTemplate and uses a Button as the content presenter. The XAML then binds the Content property of the Button to the data that would be shown in the Cell.

    In XAML:

    	<!--
    	To customize the contents of a cell, simply create a new Style
    	which targets the CellValuePresenter control.  By supplying your
    	own contents for the ControlTemplate for this control, you have 
    	complete control over the visual aspect of the cells. -->
    	<Style TargetType="{x:Type igDP:CellValuePresenter}" 
    	  x:Key="myCustomFieldCell">
    		<Setter Property="Template">
    			<Setter.Value>
    				<ControlTemplate 
    				  TargetType="{x:Type igDP:CellValuePresenter}">
    					<!-- 
    					You can bind the value of 
    					the individual cell to properties
    					of the objects in the template 
    					by using the Content property -->
    					<Button 
    					  Content="{TemplateBinding Content}" />
    				</ControlTemplate>
    			</Setter.Value>
    		</Setter>
    	</Style>
    </Grid.Resources>
    

  2. Create an instance of XamDataGrid and name it.

    In XAML:

    <igDP:XamDataGrid x:Name="XamDataGrid1" 
      ...
      >
          ...
    </igDP:XamDataGrid>
    

  3. Inside xamDataGrid's FieldLayouts object, define the Field you want to use this custom style on, by assigning the Field's CellValuePresenterStyle property off the FieldSettings.

    In XAML:

    ...
    <igDP:XamDataGrid.FieldLayouts>
    	<igDP:FieldLayout>
    		<igDP:FieldLayout.Fields>
    			<!--
    			By using the FieldSettings CellValuePresenterStyle
    			property you can target a specific Style to a Field. -->
    			<igDP:Field Name="ProductName">
    				<igDP:Field.Settings>
    					<igDP:FieldSettings 
    					  CellValuePresenterStyle="{StaticResource myCustomFieldCell}" />
    					</igDP:Field.Settings>
    				</igDP:Field>
    		</igDP:FieldLayout.Fields>
    	</igDP:FieldLayout>
    </igDP:XamDataGrid.FieldLayouts>
    ...
    

  4. Build and run the project. You should see Buttons in the ProductName Field similar to the image below.