Reputation: 717
I have been using this code, in order to determine the current row being edited interactively:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyModel myModel = (MyModel) dataGrid.CurrentItem;
int rowIndex = dataGrid.Items.IndexOf(myModel);
[...]
}
The limitation of this approach is that the event handler is also executed when the ComboBox selection changes programmatically. In that case, CurrentItem
is null and thus I don't know the row index.
TIA
Note: I do not really need the row index per se, I could use the Model (CurrentItem
) as well.
Edited after I solved the problem: Notice how the code above ignores the arguments (which tend to contain really important stuff!!)
Upvotes: 0
Views: 1602
Reputation: 717
This is what I was looking for...
private void ComboBoxRight_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
DataGridRow row = (DataGridRow) dataGrid.ContainerFromElement(comboBox);
int rowIndex = row.GetIndex();
MyModel gridModel = (MyModel) dataGrid.Items[rowIndex];
}
-Travis
Upvotes: 4
Reputation: 1341
You can use dateGrid.Selected index to find whether value is set from UI or code behind
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(dataGrid.Selected>=0)
{
MyModel myModel = (MyModel) dataGrid.CurrentItem;
int rowIndex = dataGrid.Items.IndexOf(myModel);
[...]
dataGrid.Selected=-1;
}
}
In the above case even thought comobox selection changed called when the value is changed from code behind, dataGrid selected index will be -1. But when the user changes it from the Ui you'll get the selected index of particular row and again it'll set to -1.
Upvotes: 0