1
votes

Below is the code behind in my view model:

private DataRow selectedErrorRow;
public DataRow SelectedErrorRow
{
    get { return selectedErrorRow; }
    set { selectedErrorRow = value; base.RaisePropertyChanged("SelectedErrorRow"); }
}

Then this in my view:

<DataGrid SelectedItem="{Binding SelectedErrorRow,Mode=TwoWay}"

The binding somewhat works... It "gets" the value when the datagrid is drawn but it never sets it when a new value is selected/highlighted.... Any ideas?

Note the item source for the datagrid is a DataTable.

EDIT: The Datagrid is in a PopUp, when the datagrid gets drawn it will get the binded value (null). However once I selected/highlight a row it will NOT 'set' anything. It will however 'set' the binded value null when its redrawn (the popup is open, i selected a row, close it,and reopen it). The thing is It never sets the value to anything but null, and it only sets it during the secound time its drawn.

3
Could you show the line of Code/XAML which sets ItemsSource of your Grid?user572559
ItemsSource="{Binding DataRowsWithErrors}" DataRowsWithErrors is a DataTableuser1145927
Looks like a DataContext issue. DataRowsWithErrors belongs to your DataContext (it's an MV property likely) do you have SelectedErrorRow located at the same logical level?user572559
I'm not 100% sure what your asking. But both DataRowsWithErrors and selectedErrorRow are located in the same ViewModel. I don't think its a DataContext issue?user1145927
put a breakpoint inside your DP Value Changed event handler for your grid and see if your its SelectedItem ever gets changed.user572559

3 Answers

5
votes

I needed to change what I was binding to. It needs to bind to a DataRowView and not a DataRow.

0
votes

I had this issue previously and figured it out myself.

It is not intuitive to know what is the issue since you cannot see the real value in debug but when you data bind a DataTable on a datagrid at first you think it has a DataTable type as source right ? Well you are wrong if you think that.

Actually the datagrid or the binding (i still don't know where it happen as i only see DataTable on my side) does a cast/convert of DataTable object like DataTable.AsDataView() to convert it to a DataView by itself so the selected item make sense to me to be DataRowView.

I know it's old question but this might shed light to those who get here and also look to set value as data source especially converters

-1
votes

Try this:

DataGrid SelectedItem="{Binding Path=SelectedErrorRow,Mode=TwoWay}

This should solve your problem.