CellContentTemplate Property
See Also  Example
Xceed.Wpf.DataGrid Assembly > Xceed.Wpf.DataGrid Namespace > Column Class : CellContentTemplate Property

Gets or sets the DataTemplate used to display the cells' content.

Syntax

Visual Basic (Declaration) 
Public Property CellContentTemplate As DataTemplate
C# 
public DataTemplate CellContentTemplate {get; set;}

Return Value

A reference to the DataTemplate used to display the cells' content. By default, a null reference (Nothing in Visual Basic).

Example

All examples in this topic assume that the grid is bound to the Products table of the Northwind database, unless stated otherwise.
The following example demonstrates how to provide a new CellContentTemplate, using property element syntax, for a boolean column that displays a check mark when the cell's value is true, and an x when it is false.
XAMLCopy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"> 
  <Grid.Resources> 
    <xcdg:DataGridCollectionViewSource x:Key="cvs_products" 
                                    Source="{Binding Source={x:Static Application.Current}, 
                                                      Path=Products}"/> 
  </Grid.Resources> 
   <xcdg:DataGridControl x:Name="ProductsGrid" 
                         ItemsSource="{Binding Source={StaticResource cvs_products}}"> 
      <xcdg:DataGridControl.Columns> 
 
        <xcdg:Column FieldName="Discontinued"> 
           <xcdg:Column.CellContentTemplate> 
              <DataTemplate> 
                 <Image x:Name="img" Source="D:\true.png" Stretch="None" /> 
                    <DataTemplate.Triggers> 
                       <DataTrigger Binding="{Binding}" Value="False"> 
                         <Setter TargetName="img" Property="Source" Value="D:\false.png" /> 
                       </DataTrigger> 
                    </DataTemplate.Triggers> 
              </DataTemplate> 
           </xcdg:Column.CellContentTemplate> 
        </xcdg:Column> 
      </xcdg:DataGridControl.Columns> 
   </xcdg:DataGridControl> 
</Grid>
The following example demonstrates how to format a cell's content to display a numeric value as a currency value. Although it might be tempting to apply the converter to a column's DisplayMemberBinding, this is not the recommended location as not only will the cells' content be formatted but the data type of the cells' content will be changed to the converter's.
XAMLCopy Code
<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" 
      xmlns:local="clr-namespace:Xceed.Wpf.Documentation"> 
  <Grid.Resources> 
    <xcdg:DataGridCollectionViewSource x:Key="cvs_orderdetails" 
                                    Source="{Binding Source={x:Static Application.Current},  
                                                      Path=OrderDetails}"/>            
            
 
    <local:CurrencyConverter x:Key="currencyConverter"/> 
 
    <DataTemplate x:Key="currency_celltemplate"> 
      <TextBlock Text="{Binding Converter={StaticResource currencyConverter}}"/> 
    </DataTemplate> 
  </Grid.Resources> 
 
  <xcdg:DataGridControl x:Name="OrderDetailGrid" 
                        ItemsSource="{Binding Source={StaticResource cvs_orderdetails}}"> 
    <xcdg:DataGridControl.Columns> 
 
      <xcdg:Column FieldName="UnitPrice" 
                   CellContentTemplate="{StaticResource currency_celltemplate}"/> 
    </xcdg:DataGridControl.Columns> 
  </xcdg:DataGridControl> 
</Grid>
The following code provides the implementation of the CurrencyConverter class used in the previous example.
Visual BasicCopy Code
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Data
Imports System.Globalization
Namespace Xceed.Wpf.Documentation

  <ValueConversion(GetType(Double), GetType(String))> _
  Public Class CurrencyConverter
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, _
                          ByVal parameter As Object, ByVal culture As CultureInfo) As Object
      If (Not value Is Nothing) AndAlso ((Not Object.Equals(String.Empty, value))) Then
        Try
          ' Convert the string value provided by an editor to a double before formatting.
          Dim tempDouble As Double = System.Convert.ToDouble(value, _
                                                             CultureInfo.CurrentCulture)
          Return String.Format(CultureInfo.CurrentCulture, "{0:C}", tempDouble)
        Catch e1 As FormatException
        Catch e2 As OverflowException
        End Try
      End If
      Return String.Format(CultureInfo.CurrentCulture, "{0}", value)
    End Function
    Public Function ConvertBack(ByVal value As Object, _
                                ByVal targetType As Type, _
                                ByVal parameter As Object, _
                                ByVal culture As CultureInfo) As Object
      Return Double.Parse(TryCast(value, String), _
                          NumberStyles.Currency, _
                          CultureInfo.CurrentCulture)
    End Function
  End Class
End Namespace
The following code provides the implementation of the CurrencyConverter class used in the previous example.
C#Copy Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Globalization;
namespace Xceed.Wpf.Documentation
{
 [ValueConversion(
typeof( double ), typeof( string ) )]
 
public class CurrencyConverter : IValueConverter
 {
   
public object Convert( object value, Type targetType,
                          
object parameter, CultureInfo culture )
   {
     
if( ( value != null ) && ( !object.Equals( string.Empty, value ) ) )
     {
       
try
       {
         
// Convert the string value provided by an editor to a double before formatting.
         
double tempDouble = System.Convert.ToDouble( value, CultureInfo.CurrentCulture );
         
return string.Format( CultureInfo.CurrentCulture, "{0:C}", tempDouble );
       }
       
catch( FormatException )
       {
       }
       
catch( OverflowException )
       {
       }
     }
     
return string.Format( CultureInfo.CurrentCulture, "{0}", value );
   }
   
public object ConvertBack( object value, Type targetType,
                              
object parameter, CultureInfo culture )
   {
     
return double.Parse( value as string, NumberStyles.Currency,
                          CultureInfo.CurrentCulture );
   }
 }
}

Requirements

Supported Frameworks: Microsoft .NET Framework version 3.5

See Also