Back to Devexpress

SpreadsheetControl.CellTemplate Property

wpf-devexpress-dot-xpf-dot-spreadsheet-dot-spreadsheetcontrol.md

latest5.8 KB
Original Source

SpreadsheetControl.CellTemplate Property

Gets or sets the template used to visualize worksheet cells. This is a dependency property.

Namespace : DevExpress.Xpf.Spreadsheet

Assembly : DevExpress.Xpf.Spreadsheet.v25.2.dll

NuGet Package : DevExpress.Wpf.Spreadsheet

Declaration

csharp
public DataTemplate CellTemplate { get; set; }
vb
Public Property CellTemplate As DataTemplate

Property Value

TypeDescription
DataTemplate

A DataTemplate object that defines the presentation of worksheet cells.

|

Remarks

The cell template allows you to completely change the look and feel of worksheet cells while maintaining their present behavior.

Use the CellTemplate property to define a template that specifies the visualization of worksheet cells. The binding source for the CellTemplate template is represented by the DevExpress.Xpf.Spreadsheet.CellData class.

The image and sample code below demonstrate how to use a custom cell template to display a cell formula in a callout at the top right corner of the cell.

xaml
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxsps="http://schemas.devexpress.com/winfx/2008/xaml/spreadsheet" 
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
    x:Class="CellTemplateExample.MainWindow"
    dx:ThemeManager.ThemeName="Office2013"
        Title="MainWindow" Height="600" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="FormulaTemplate" DataType="{x:Type dxsps:CellData}">
            <Grid>
                <Canvas HorizontalAlignment="Right">
                    <Grid Canvas.Left="0" Canvas.Top="-20" Height="26">
                        <Border Background="Lavender" Height="14" VerticalAlignment="Top">
                            <TextBlock Margin="10,0" Text="{Binding Path=Cell.Formula}" 
                                       FontWeight="Bold" Foreground="Brown" VerticalAlignment="Center"/>
                        </Border>
                        <Path VerticalAlignment="Top" Margin="0,14,0,0" HorizontalAlignment="Left" 
                              Data="M 0,0 0,10 7,0" Fill="Lavender" />
                    </Grid>
                </Canvas>
                <TextBlock Text="{Binding TextSettings.Text}" TextWrapping="{Binding TextSettings.TextWrapping}" 
                           FontFamily="{Binding TextSettings.FontFamily}"
                           FontStyle="{Binding TextSettings.FontStyle}" FontSize="{Binding TextSettings.FontSize}" 
                           FontWeight="{Binding TextSettings.FontWeight}" 
                           TextAlignment="{Binding TextSettings.TextAlignment}" 
                           Foreground="Black" Margin="0,0,0,2" Clip="{Binding Clip}"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <dxsps:SpreadsheetControl x:Name="spreadsheetControl1"/>
    </Grid>
</Window>
csharp
using System.Windows;

namespace CellTemplateExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            spreadsheetControl1.LoadDocument("vlookup.xlsx");
            #region #celltemplate_code
            spreadsheetControl1.CellTemplate = 
                spreadsheetControl1.TryFindResource("FormulaTemplate") as DataTemplate;
            #endregion #celltemplate_code
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System.Windows

Namespace CellTemplateExample
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
            spreadsheetControl1.LoadDocument("vlookup.xlsx")
' #Region "#celltemplate_code"
            spreadsheetControl1.CellTemplate = TryCast(spreadsheetControl1.TryFindResource("FormulaTemplate"), DataTemplate)
' #End Region ' #celltemplate_code
        End Sub
    End Class
End Namespace
xaml
<Application x:Class="CellTemplateExample.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows

Namespace CellTemplateExample
    ''' <summary>
    ''' Interaction logic for App.xaml
    ''' </summary>
    Partial Public Class App
        Inherits Application
    End Class
End Namespace

See Also

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.Xpf.Spreadsheet Namespace