I have some DataGrid, data is bind to my collection (INotifyCollectionChanged
).
Records has to be prefilled when you create new one. So I disabled adding in dataGrid (CanUserAddRows="False"
) and handle “PreviewKeyUp” event (when user press ctrl+N, execute method “CreateRecord” – notify of added new record).
This scenario work perfectly… almost.
PreviewKeyUp
was fired only if some element in grid is focused (e.g. DataGridRow). But if I don’t have any record, there is no focus and event was never fired.
So my question is: If is it possible to somehow handle keys ctrl+N when DataGrid is empty? E.g. trigging Focusable or something like this.
Another workaround would be to “catch” somehow adding row and fill required fields, but...
Standard NewItemPlaceholder will be fine, but EditableItems use item constructor with no parameters.
And this is another think I couldn't use (or I just don't known how :D)
My Data has to be created with predefined values (like createdUserId).
Some code to clarify:
public MyItemsCollection Items { get; set; }
public MyItemType CreateRecord()
{
MyItemType item = new MyItemType(userId);
item.InitValues();
Items.Add(item);
return item;
}
/********/
public void dataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.N && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
CreateRecord();
e.Handled = true;
}
}
<DataGrid AutoGenerateColumns="False" Name="dataGrid" ItemsSource="{Binding Items}"
CanUserSortColumns="False" CanUserDeleteRows="True"
CanUserAddRows="False" PreviewKeyUp="dataGrid_PreviewKeyUp">
Any one has some idea?