title: WPF Datagrid Get Cell Value in MVVM architecture
body: <p>How to get selected Cell value from Datagrid? </p>
  
  <p><strong>What information i have looked?</strong>   Everything where title was "WPF get cell value MVVM".</p>
  
  <p><strong>What i did?</strong> 1 step:</p>
  
  <pre><code>    &lt;Page x:Class="PDB.UsersView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:PDB"
        xmlns:PDB ="clr-namespace:PDBapi;assembly=PDBapi"
        mc:Ignorable="d" 
        d:DesignHeight="450" d:DesignWidth="800"
        Title="UsersView"
        &gt;
  
      &lt;Page.DataContext&gt;
          &lt;PDB:UsersViewModel/&gt;
      &lt;/Page.DataContext&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto"/&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;!--Page Header info content--&gt;
          &lt;Grid Grid.Row="0"&gt;
              &lt;TextBlock Text="{Binding ElementName=myGrd, Path=CurrentCell.Column.DisplayIndex}"/&gt;
          &lt;/Grid&gt;
          &lt;!--Datagrid content--&gt;
          &lt;DataGrid x:Name="myGrd" 
                    SelectionMode="Single"
                    SelectionUnit="Cell"
                    IsReadOnly="True"
                    Grid.Row="1" 
                    ItemsSource="{Binding Users}" 
                    AutoGenerateColumns="True"
  
                    CanUserAddRows="False"&gt;
              &lt;DataGrid.InputBindings&gt;
                  &lt;MouseBinding MouseAction="RightClick"
                    Command="{Binding CellClickCommand}"
                    CommandParameter="{Binding ElementName=myGrd, Path=CurrentCell}" /&gt;
              &lt;/DataGrid.InputBindings&gt;
  
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>VM:</p>
  
  <pre class="lang-cs prettyprint-override"><code>        public  UsersViewModel()
          {
              UserList = new ObservableCollection&lt;User&gt;();
  
              GetUsers();
  
              Users = CollectionViewSource.GetDefaultView(UserList);
  
              CellClickCommand = new RelayParamCommand((data) =&gt; GetValue(data));
          }
  
          public void GetUsers()
          {
              User user = new User();
  
              // Users = await user.GetUsers();
              UserList.Add(new User
              {
                  Name = "Marius",
                  Departament = "some",
                  Tabelis = 5
              });
  
              UserList.Add(
              new User
              {
                  Name = "Darius",
                  Departament = "unknown",
                  Tabelis = 20
              });
          }
  
          private void GetValue(object data)
          {
  
              var some = (DataGridCellInfo)data;
  
              //Returns only number
              Console.WriteLine(some.Column?.DisplayIndex.ToString());
          }
      }
  </code></pre>
  
  <p>But with this approach i faced 2 issues:
  In xaml page i added textblock for testing which text was binded to datagrid currentCell. When i click right mouse button it shows int value correctly. But in my GetValue(object data) function console return null at first right click and from second time returns int value, but value in console is always diferent from textblock value, i have to click two times on same cell to get right cell position. That is completly wrong. How to solve that?</p>
  
  <p>Another issue:  How  to get real value from currentCell i have binded? </p>
  
  <p><strong>What i did?</strong> 2 step:</p>
  
  <p>In xaml  i binded datagrid  currentCell to VM property <code>CurrentCell="{Binding Cell}"</code> 
  I got value it was ok, but still it returns only  DataGridCellInfo object. I tried to cast to Users object and various things but i failed to get value of cell.
  Can someone provide good practice to get cell value from datagrid? </p>