Back to Devexpress

CellData Class

wpf-devexpress-dot-xpf-dot-spreadsheet-447fc6c6.md

latest9.0 KB
Original Source

CellData Class

Contains information about a worksheet cell.

Namespace : DevExpress.Xpf.Spreadsheet

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

NuGet Package : DevExpress.Wpf.Spreadsheet

Declaration

csharp
public class CellData :
    INotifyPropertyChanged
vb
Public Class CellData
    Implements INotifyPropertyChanged

Remarks

The CellData object contains information about a worksheet cell and serves as a data context (binding source) for the cell template.

The following example uses the CellData.Cell property to obtain the cell reference and content used to select a proper cell template.

This example uses a template selector to display formulas above certain cells. If a cell does not contain a formula, it displays a ‘Missing Formula’ warning.

The result is shown below.

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"
    xmlns:local="clr-namespace:CellTemplateSelectorExample"
    x:Class="CellTemplateSelectorExample.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>
        <DataTemplate x:Key="EmptyTemplate" DataType="{x:Type dxsps:CellData}">
            <Grid>
                <Canvas HorizontalAlignment="Right">
                    <Grid Canvas.Left="0" Canvas.Top="-20" Height="26">
                        <Border Background="LightPink" Height="14" VerticalAlignment="Top">
                            <TextBlock Margin="5,0" Text="Missing Formula" FontWeight="Bold" VerticalAlignment="Center"/>
                        </Border>
                        <Path VerticalAlignment="Top" Margin="0,14,0,0" HorizontalAlignment="Left" Data="M 0,0 0,10 7,0" Fill="LightPink"/>
                    </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="Red" Margin="0,0,0,2" Clip="{Binding Clip}"/>
                <Border Background="Red" VerticalAlignment="Bottom" Height="2" Clip="{Binding Clip}"/>
            </Grid>
        </DataTemplate>
        <local:CellTemplateSelector x:Key="CellTemplateSelector"/>
    </Window.Resources>
    <Grid>
        <dxsps:SpreadsheetControl x:Name="spreadsheetControl1"/>
    </Grid>
</Window>
xaml
<Application x:Class="CellTemplateSelectorExample.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 DevExpress.Xpf.Spreadsheet
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls

Namespace CellTemplateSelectorExample
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
            spreadsheetControl1.LoadDocument("vlookup.xlsx")
' #Region "#celltemplate_code"
            spreadsheetControl1.CellTemplateSelector = TryCast(spreadsheetControl1.TryFindResource("CellTemplateSelector"), DataTemplateSelector)
' #End Region ' #celltemplate_code
        End Sub
    End Class

    Public Class CellTemplateSelector
        Inherits DataTemplateSelector
        Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
            Dim data As CellData = TryCast(item, CellData)
            Return If(CanApplyCustomTemplate(data.Cell.GetReferenceA1()), GetCellTemplate(data.Cell, CType(container, SpreadsheetControl)), MyBase.SelectTemplate(item, container))
        End Function
        Private customTemplateCells As New List(Of String) (New String() {"A2", "A3", "A4", "A5", "A6"})
        Private Function CanApplyCustomTemplate(ByVal cellPosition As String) As Boolean
            Return customTemplateCells.Contains(cellPosition)
        End Function
        Private Function GetCellTemplate(ByVal cell As DevExpress.Spreadsheet.Cell, ByVal control As SpreadsheetControl) As DataTemplate
            Dim templateName As String = If(cell.HasFormula, "FormulaTemplate", "EmptyTemplate")
            Return TryCast(control.TryFindResource(templateName), DataTemplate)
        End Function
    End Class
End Namespace
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 CellTemplateSelectorExample
    ''' <summary>
    ''' Interaction logic for App.xaml
    ''' </summary>
    Partial Public Class App
        Inherits Application
    End Class
End Namespace
csharp
using DevExpress.Xpf.Spreadsheet;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace CellTemplateSelectorExample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            spreadsheetControl1.LoadDocument("vlookup.xlsx");
            #region #celltemplate_code
            spreadsheetControl1.CellTemplateSelector = spreadsheetControl1.TryFindResource("CellTemplateSelector") as DataTemplateSelector;
            #endregion #celltemplate_code
        }
    }

    public class CellTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            CellData data = item as CellData;
            return CanApplyCustomTemplate(data.Cell.GetReferenceA1()) ? GetCellTemplate(data.Cell, (SpreadsheetControl)container) : base.SelectTemplate(item, container);
        }
        List<string> customTemplateCells = new List<string>() { "A2", "A3", "A4", "A5", "A6" };
        private bool CanApplyCustomTemplate(string cellPosition)
        {
            return customTemplateCells.Contains(cellPosition);
        }
        private DataTemplate GetCellTemplate(DevExpress.Spreadsheet.Cell cell, SpreadsheetControl control)
        {
            string templateName = cell.HasFormula ? "FormulaTemplate" : "EmptyTemplate";
            return control.TryFindResource(templateName) as DataTemplate;
        }
    }
}

Inheritance

Object CellData

See Also

CellData Members

DevExpress.Xpf.Spreadsheet Namespace