开发者

Focus problems in datagrid

I am working on an accounting application, My users don't want to touch mouse at all as it will decrease their speed...So focusing is very important in the application. I a开发者_如何转开发m using a datagrid to display some data, what they want is as soon as the window is loaded and data filled in the datagrid they should be able to move around in the data-grid by using arrow keys and invoe a command by pressing Enter...i.e, The keyboard focus should be at the first row or first cell. I have tried almost everything but nothing seems to work here, right now they have to press TAB once to go inside the data-grid even if the the last row is shown as selected in the window I mean last row gets selected but in order to moce up and down in the datagrid they have to press TAB once....I dnot understand what is the problem here...here is the code I am using in loaded event of Windows to set the UI:

dataGrid1.SelectedIndex = dataGrid1.Items.Count -1;
dataGrid1.CurrentItem = dataGrid1.SelectedItem;

dataGrid1.Focus();
dataGrid1.ScrollIntoView(dataGrid1.CurrentItem);


Set SelectedIndex="0" for the DataGrid and subsribe to the Loaded event. In the event handler you move the focus to the first row/cell by calling MoveFocus

Xaml

<DataGrid ...
          SelectedIndex="0"
          Loaded="DataGrid_Loaded">

Code behind event handler

private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    dataGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}


Try this

//set current cell which you want to edit
dataGrid1.CurrentCell = new DataGridCellInfo(dataGrid1.Items[0], dataGrid1.Columns[0]);
//start editing it
dataGrid1.BeginEdit();

I execute this code inside DataGrid's loaded event and works fine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜