Rubel
Rubel

Reputation: 71

Datagrid cell focus xaml

How to disable next cell focusing after adding new item in datagrid? Please note that I am doing my application in MVVM pattern.

Upvotes: 1

Views: 320

Answers (1)

ViVi
ViVi

Reputation: 4464

You can’t disable the selection of the next item. It is the intended behavior of the datagrid. I hope you want a behavior like when user clicks on some row it should be selected and when a new value is added you don’t want the selection to change automatically. For that the best thing to do is to bind the SelectedInex of the datagrid to some property and then set it manually when the selection changes when a data is updated. You could also set the required behavior in the setter of SelectedIndex property too.

Actually there are a few way to select items in the DataGrid. It just depends which one works best for the situation

First and most basic is SelectedIndex this will just select the Row at that index in the DataGrid

 <DataGrid SelectedIndex="{Binding SelectedIndex}" />

private int _selectedIndex;
public int SelectedIndex
{
    get { return _selectedIndex; }
    set { _selectedIndex = value; NotifyPropertyChanged("SelectedIndex"); }
}

SelectedIndex = 2;

SelectedItem will select the row that matches the row you set

<DataGrid SelectedItem="{Binding SelectedRow}" />

private DataRow _selectedRow;
public DataRow SelectedRow
{
    get { return _selectedRow; }
    set { _selectedRow = value; NotifyPropertyChanged("SelectedRow");}
}

SelectedRow = items.First(x => x.whatever == something);

Upvotes: 1

Related Questions