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.
Is this possible? 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.
I managed to find a way that suited my needs (reverse tabbing still doesn't work).
Here is one of my columns (I decided NO to textblocks - and have a single cell template):
<my:DataGridTemplateColumn Header="Item #">
<my:DataGridTemplateColumn.Cel
lTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ItemNumber}" />
</DataTemplate>
</my:DataGridTemplateColumn.Ce
llTemplate>
</my:DataGridTemplateColumn>
I also have similar column for a CheckBox (I didn't like the built in column).
The changes a similar to the SingleClick:
<Style TargetType="{x:Type my:DataGridCell}">
<EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus
"></EventSetter>
</Style>
Private Sub DataGridCell_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs
)
Dim cell As DataGridCell = CType(sender, DataGridCell)
Dim childTxt As TextBox = FindVisualChild(Of TextBox)(cell)
If childTxt IsNot Nothing Then
If Not childTxt.IsFocused Then
Keyboard.Focus(childTxt)
End If
Exit Sub
End If
Dim childChk As CheckBox = FindVisualChild(Of CheckBox)(cell)
If childChk IsNot Nothing Then
If Not childChk.IsFocused Then
Keyboard.Focus(childChk)
End If
Exit Sub
End If
End Sub
Private Shared Function FindVisualChild(Of T As UIElement)(ByVal element As UIElement) As T
Dim child As UIElement = element
While child IsNot Nothing
Dim correctlyTyped As T = TryCast(child, T)
If correctlyTyped IsNot Nothing Then Return correctlyTyped
If VisualTreeHelper.GetChildrenCo
unt(child) < 1 Then Exit While
child = CType(VisualTreeHelper.GetChil
d(child, 0), UIElement)
End While
Return Nothing
End Function
You would have to modify the GotFocus event handler to suit your individual needs.