DataGridCell Custom Control

Aug 24, 2010 at 8:39 PM

Hello everyone,

I want to add some features to the DataGridCell class and  I have created a new class deriving from DataGridCell. I have also added default style in the Themes/Generic.xaml resource dictionary.

The runtime is throwing an exception:

'MyDataGridCell' TargetType does not match type of element 'DataGridCell'.

Does anyone know what am I doing wrong here? Thank you, A-

 This is the DataGridCell style located in Generic.xaml file:
<Style TargetType="{x:Type c:MyDataGridCell}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}"/>
        <Setter Property="Padding" Value="4,3,4,3" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type c:MyDataGridCell}">
                        <Border Padding="{TemplateBinding Padding}" 
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                                <ContentPresenter />
                        </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Lavender" />
                <Setter Property="BorderBrush" Value="Lavender" />
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="Background" Value="LightGray" />
                <Setter Property="BorderBrush" Value="Red" />
                <Setter Property="BorderThickness" Value="2" />
            </Trigger>
        </Style.Triggers>
    </Style>

This is the MyDataGridCell class:
    public class MyDataGridCell : DataGridCell
    {
        static MyDataGridCell()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyDataGridCell),
                new FrameworkPropertyMetadata(typeof(MyDataGridCell)));
        }


        public bool IsFillCursorVisible
        {
            get; set;
        }
    }

And here's the grid control xaml declaration:

 [...]
<Style x:Key="MyDataGridCellStyle" TargetType="{x:Type c:MyDataGridCell}"/> [...]
  <myGrid:MyDataGrid x:Name="grid" CellStyle="{StaticResource MyDataGridCellStyle}"/>