在按键(键入)事件

时间:2015-06-24 20:04:39

标签: wpf vb.net datagrid

我有一个我创建的DataGridTemplateColumn,没什么了

 Public Class DataGridAutoCompleteColumn
        Inherits DataGridTemplateColumn

        Protected Overrides Function PrepareCellForEdit(editingElement As FrameworkElement, editingEventArgs As RoutedEventArgs) As Object
            editingElement.MoveFocus(New TraversalRequest(FocusNavigationDirection.First))
            Return MyBase.PrepareCellForEdit(editingElement, editingEventArgs)
        End Function
    End Class

我也有一个自动完成控制(效果很好) 我在这个栏目中使用它像这样

                     <DataGrid.Resources>
                        <DataTemplate x:Key="EditingDateTemplate">
                            <Auto:AutoCompleteTextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
                                                      x:Name="EditingBox" 
                                                      Text="{Binding Path=Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  ItemsSource="{Binding Source={StaticResource MyComplList}}"/>                               
                        </DataTemplate>
                        <DataTemplate x:Key="NormalCellDataTemplate">
                            <TextBlock Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged,FallbackValue=Add New ...}"/>
                        </DataTemplate>
                    </DataGrid.Resources>
                    <DataGrid.Columns>
                        <Auto:DataGridAutoCompleteColumn
                            CellTemplate="{StaticResource NormalCellDataTemplate}" 
                            CellEditingTemplate="{StaticResource EditingDateTemplate}" 
                            Header="Test Auto Complete"/>
                    </DataGrid.Columns>

聚焦机制,每件事都运作良好,但我面临一个问题 “编辑”

当双击单元格时,它进入编辑模式但是当我开始打字而不是双击它时(不像DataGridTextColumn那样)

所以我在datagrid

中的keyup事件中为此做了一个解决方法
 Private Sub MyDataGrid_KeyUp(sender As Object, e As KeyEventArgs)
    Dim ignorablekeys() As Key = {Key.Up, Key.Down, Key.Left, Key.Right, Key.Delete, Key.Return, Key.LeftShift, Key.RightShift, Key.LeftCtrl, Key.RightCtrl, Key.LeftAlt, Key.RightAlt}
    If Not ignorablekeys.Contains(e.Key) Then
        If TypeOf e.OriginalSource Is DataGridCell Then
            Dim cell = DirectCast(e.OriginalSource, DataGridCell)
            If Not cell.IsReadOnly Then
                Dim datagrid = DirectCast(sender, DataGrid)
                datagrid.BeginEdit()
                'Push the entered Text Into the cell <<<<< Problem >>>>>
                 Dim e1 = New KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, e.Key)
                 e1.RoutedEvent = KeyUpEvent
                 InputManager.Current.ProcessInput(e1)
            End If
        End If
    End If

End Sub
问题是,它不起作用,它不发送文本

0 个答案:

没有答案