I have DataGrid and I am trying to get the cell details (column,row and value) of a selected cell using MVVM and avoid putting in any changes to the code behind.
My data grid looks like this
<DataGrid x:Name="MyDataGrid" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=MyData}" AutoGenerateColumns="False" IsReadOnly="True" GridLinesVisibility="None" SelectionUnit="Cell">
<DataGrid.Columns>
<DataGridTextColumn Header="a" Width="130" Binding="{Binding Path=A}"/>
<DataGridTextColumn Header="b" Width="100" Binding="{Binding Path=B}"/>
<DataGridTextColumn Header="c" Width="100" Binding="{Binding Path=C}"/>
</DataGrid.Columns>
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DataGridLeftClick}" CommandParameter="{Binding ElementName=MyDataGrid, Path=SelectedCells}" />
</DataGrid.InputBindings>
in my ViewModel I have
DataGridLeftClick = new RelayCommand(LeftClick);
...
public ICommand DataGridLeftClick { get; private set; }
public void LeftClick(object obj)
{
log.Info(obj);
}
The command works but there are two things the value of obj is always null. If I remove the SelectionUnit of the grid (so i assume it defaults to FullRow) then I get the data of the full rows cells. How do I get the cell row,index and value of a single the cell that was clicked on?
Thanks, Nick