Single-Click Editing from data grid style

Jun 4, 2010 at 12:45 AM
Edited Jun 4, 2010 at 12:49 AM

I placed the style shown above in a new Style that I've defined in a resource dictionary but when I try to build I get the following error.

The event 'PreviewMouseLeftButtonDown' cannot be specified on a Target tag in a Style. Use an EventSetter instead.

I'm attempting to wrap the DataGrid up with a control that inherits from the WpfToolkit DataGrid so that I can add the functionality described here to a single control that I can reuse throughout different portions of my app. Below is the style that I'm attempting to create for my own MyDataGrid control that inherits from DataGrid.

 

 

    <Style xmlns:wtk="http://schemas.microsoft.com/wpf/2008/toolkit"
           TargetType="{x:Type local:MyDataGrid}"
           BasedOn="{StaticResource {x:Type wtk:DataGrid}}">
        <Style.Resources>


            <!--
            SINGLE CLICK EDITING
            -->
            <Style TargetType="{x:Type wtk:DataGridCell}">
                <EventSetter Event="PreviewMouseLeftButtonDown"
                             Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
            </Style>


            <!--
            COLUMN SELECTION:
            These resources add a button to the column header that will select
            all of the cells in that column.
            -->
            <Style x:Key="ColumnHeaderGripperStyle"
                   TargetType="{x:Type Thumb}">
                <Setter Property="Width"
                        Value="8" />
                <Setter Property="Background"
                        Value="Transparent" />
                <Setter Property="Cursor"
                        Value="SizeWE" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Thumb}">
                            <Border Padding="{TemplateBinding Padding}"
                                    Background="{TemplateBinding Background}" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="SelectColumnButtonStyle"
                   TargetType="{x:Type Button}">
                <Setter Property="VerticalAlignment"
                        Value="Bottom" />
                <Setter Property="Height"
                        Value="10" />
                <Setter Property="ClickMode"
                        Value="Press" />
                <Setter Property="Cursor"
                        Value="ScrollS" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border Background="Transparent" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <EventSetter Event="PreviewMouseLeftButtonDown"
                             Handler="DataGridCell_PreviewMouseLeftButtonDown" />
            </Style>

            <Style x:Key="{x:Type wtk:DataGridColumnHeader}"
                   TargetType="{x:Type wtk:DataGridColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type wtk:DataGridColumnHeader}">
                            <Grid>
                                <wtk:DataGridHeaderBorder SortDirection="{TemplateBinding SortDirection}"
                                                          IsHovered="{TemplateBinding IsMouseOver}"
                                                          IsPressed="{TemplateBinding IsPressed}"
                                                          IsClickable="{TemplateBinding CanUserSort}"
                                                          Background="{TemplateBinding Background}"
                                                          BorderBrush="{TemplateBinding BorderBrush}"
                                                          BorderThickness="{TemplateBinding BorderThickness}"
                                                          Padding="{TemplateBinding Padding}">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                                </wtk:DataGridHeaderBorder>

                                <Thumb x:Name="PART_LeftHeaderGripper"
                                       HorizontalAlignment="Left"
                                       Style="{StaticResource ColumnHeaderGripperStyle}" />
                                <Thumb x:Name="PART_RightHeaderGripper"
                                       HorizontalAlignment="Right"
                                       Style="{StaticResource ColumnHeaderGripperStyle}" />
                                <Button x:Name="SelectColumnButton"
                                        Style="{StaticResource SelectColumnButtonStyle}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Style.Resources>
    </Style>