5
votes

I'm learning WPF MVVM pattern. I'm stuck in Binding CurrentCell of datagrid. Basically I need the row index and column index of current cell.

<DataGrid AutoGenerateColumns="True" 
          SelectionUnit="Cell" 
          CanUserDeleteRows="True" 
          ItemsSource="{Binding Results}" 
          CurrentCell="{Binding CellInfo}" 
          Height="282" 
          HorizontalAlignment="Left" 
          Margin="12,88,0,0" 
          Name="dataGrid1" 
          VerticalAlignment="Top" 
          Width="558" 
          SelectionMode="Single">

Here is my ViewModel

private User procedureName = new User();

public  DataGridCell   CellInfo
{
    get { return procedureName.CellInfo; }
    //set
    //{
    //    procedureName.CellInfo = value;
    //    OnPropertyChanged("CellInfo");
    //}
}

Here is my Model

private DataGridCell cellInfo;

public DataGridCell CellInfo
{
    get { return cellInfo; }
    //set
    //{
    //    cellInfo = value;
    //    OnPropertyChanged("CellInfo");
    //}
}

And in my ViewModel CellInfo is always null. I am not able to get the value from the currentcell in datagrid. Please let me know a way to getCurrentCell in the ViewModel.

if (CellInfo != null)
{
    MessageBox.Show("Value is" + CellInfo.Column.DisplayIndex.ToString());
}
1
CurrentCell="{Binding CellInfo}" - Try setting this thing to binding mode two way.dev hedgehog
why do you need cell info, can't you bind to a selected item of the data grid and extract the properties you need from there? Also, calling the messagebox from your viewmodel is going to create problems. Was that just for testing?J King
Thanks.I tried with SelectedItem.Even selecteditem is null in my view model.yes messagebox is just for testing.Bharath T
@devhedgehog I tried with mode two way also.but i didnt get the CurrentCell in my View model.Bharath T

1 Answers

14
votes

After having a quick poke-around I've noticed a very simple solution to your problem.

First of all there's two problems rather then one here. You cannot bind a CellInfo of a type DataGridCell, it needs to be DataGridCellInfo as xaml cannot convert it on its own.

Secondly in your xaml you will need to add Mode=OneWayToSource or Mode=TwoWay to your CellInfo binding.

Here is a rough example semi-related to your original code

XAML

<DataGrid AutoGenerateColumns="True"
          SelectionUnit="Cell"
          SelectionMode="Single"
          Height="250" Width="525" 
          ItemsSource="{Binding Results}"
          CurrentCell="{Binding CellInfo, Mode=OneWayToSource}"/>

VM

private DataGridCellInfo _cellInfo;
public DataGridCellInfo CellInfo
{
    get { return _cellInfo; }
    set
    {
        _cellInfo = value;
        OnPropertyChanged("CellInfo");
        MessageBox.Show(string.Format("Column: {0}",
                        _cellInfo.Column.DisplayIndex != null ? _cellInfo.Column.DisplayIndex.ToString() : "Index out of range!"));
    }
}

Just a small tip - if you debug your app and look at the Output window it actually tells you if there is any troubles with your bindings.

Hope this helps!

K.