title: How can I detect in a DataGrid if row is clicked and change its column from true to false and vice versa?
body: <p>I'm working on a small WPF application, when I click on a row I'm making my checkbox column selected/unselected. This is how my rows look:</p>
  
  <p><a href="https://i.stack.imgur.com/J1BMH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J1BMH.png" alt="enter image description here"></a></p>
  
  <p>And here is my code:</p>
  
  <pre><code>private void dtgTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
      if (dtgTest.SelectedItem != null)
      {
          CheckBox checkbocColumn = (dtgTest.Columns[3].GetCellContent(dtgTest.SelectedItem) as CheckBox);
          checkbocColumn.IsChecked = !checkbocColumn.IsChecked;
  
  
          var selectedItem = (BillItemInSerie)dtgTest.SelectedItem;
          var obj = serialNumbersIn.FirstOrDefault(sn =&gt; selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
          obj.IsChecked= (bool)checkbocColumn.IsChecked;
  
      }
  }
  </code></pre>
  
  <p>Here is how I'm filling DataGrid:</p>
  
  <pre><code>public Test_Window()
     : this()
  {
      databaseValues = Controller.Instance.GetById(Id);
      dtgTest.ItemsSource = null;
      dtgTest.ItemsSource = databaseValues;
  }
  </code></pre>
  
  <p>So when form is generated I acctually get all items from DB</p>
  
  <p>And here is my XAML:</p>
  
  <pre><code>&lt;DataGrid Name="dtgTest"  IsReadOnly="True"  VirtualizingStackPanel.VirtualizationMode="Standard"  EnableColumnVirtualization = "True" EnableRowVirtualization ="True"  MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0"  HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged"&gt;
      &lt;DataGrid.CellStyle&gt;
          &lt;StaticResource ResourceKey="DataGridCentering"/&gt;
      &lt;/DataGrid.CellStyle&gt;
      &lt;DataGrid.Resources&gt;
          &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
              &lt;Setter Property="Background" Value="#0091EA"/&gt;
              &lt;Setter Property="Opacity" Value="1"/&gt;
              &lt;Setter Property="Foreground" Value="White"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
              &lt;Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/&gt;
              &lt;Setter Property="FontFamily" Value="Arial"/&gt;
              &lt;Setter Property="Height" Value="40"/&gt;
          &lt;/Style&gt;
          &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
             Color="LightBlue"/&gt;
      &lt;/DataGrid.Resources&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn         Binding="{Binding Value1}"          Header=""   Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*"  /&gt;
          &lt;DataGridTextColumn         Binding="{Binding Value2}"          Header=""   Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*"   /&gt;
          &lt;DataGridTextColumn         x:Name="colFormatedDate"            Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}"     Header=""   Foreground="Black"      FontSize="15" FontFamily="Verdana" Width="20*" /&gt;
          &lt;DataGridCheckBoxColumn     x:Name="colSelektiraj"              Binding="{Binding IsChecked}"       Header=""  Width="10*" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>But issue is here when I click on a row and make checkbox column selected (<strong>LIKE IN EXAMPLE IMAGE ABOVE</strong>) , and If I immediately change my mind and click again on selected row to change the state of checkbox column I won't be able to do it, because <code>dtgTest_SelectionChanged</code> won't trigger because I did not change selection.. </p>
  
  <p>So I guess detecting if a row is clicked might help me here? So I might execute similar code as it is in <code>dtgTest_SelectionChanged</code> event?</p>
  
  <p>Any kind of help would be awesome!
  Thanks, guys</p>
  
  <p>Cheers</p>
  
  <p><strong><em>EDIT AFTER Rekshino help:</em></strong></p>
  
  <pre><code>&lt;DataGrid Name="dtgTest" IsReadOnly="True"  VirtualizingStackPanel.VirtualizationMode="Standard"  EnableColumnVirtualization = "True" EnableRowVirtualization ="True"  MaxWidth="4000" MaxHeight="2000" Background="White" Margin="5,5,5,0" AutoGenerateColumns="False" RowHeaderWidth="0"  HorizontalGridLinesBrush="#0091EA" VerticalGridLinesBrush="#0091EA" CanUserAddRows="False" RowHeight="30" Grid.ColumnSpan="2" Grid.Row="2" SelectionChanged="dtgTest_SelectionChanged" PreviewMouseDown="dtgTest_PreviewMouseDown"&gt;
      &lt;DataGrid.CellStyle&gt;
          &lt;StaticResource ResourceKey="DataGridCentering"/&gt;
      &lt;/DataGrid.CellStyle&gt;
      &lt;DataGrid.Resources&gt;
          &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
              &lt;Setter Property="Background" Value="#0091EA"/&gt;
              &lt;Setter Property="Opacity" Value="1"/&gt;
              &lt;Setter Property="Foreground" Value="White"/&gt;
              &lt;Setter Property="HorizontalContentAlignment" Value="Center" /&gt;
              &lt;Setter Property="FontSize" Value="{x:Static local:Globals.dataGridfontSizeHeader}"/&gt;
              &lt;Setter Property="FontFamily" Value="Arial"/&gt;
              &lt;Setter Property="Height" Value="40"/&gt;
          &lt;/Style&gt;
          &lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/&gt;
          &lt;Style TargetType="DataGridCell"&gt;
              &lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="dtgTest_PreviewMouseDown"/&gt;
          &lt;/Style&gt;
      &lt;/DataGrid.Resources&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn         Binding="{Binding Value1}"          Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*"  /&gt;
          &lt;DataGridTextColumn         Binding="{Binding Value2}"          Foreground="Black" FontSize="15" FontFamily="Verdana" Width="35*"   /&gt;
          &lt;DataGridTextColumn         x:Name="colFormatedDate"            Binding="{Binding ExpireDate, StringFormat ={}{0:MM/yyyy}}"   Foreground="Black"      FontSize="15" FontFamily="Verdana" Width="20*" /&gt;
          &lt;DataGridCheckBoxColumn     x:Name="colSelektiraj"              Binding="{Binding IsChecked, NotifyOnTargetUpdated=True}"     Header=""  Width="10*" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>C# :</p>
  
  <pre><code>private void dtgTest_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  {
  
      var cell = sender as DataGridCell;
  
      if (cell == null)
      {
          return;
      }
  
      DataGridRow parGridRow = null;
      var visParent = VisualTreeHelper.GetParent(cell);
      while (parGridRow == null &amp;&amp; visParent != null)
      {
          parGridRow = visParent as DataGridRow;
          visParent = VisualTreeHelper.GetParent(visParent);
      }
      if (parGridRow == null) { return; }
  
      var selectedItem = (parGridRow.DataContext as BillItemInSerie);
      var obj = serialNumbersIn.FirstOrDefault(sn =&gt; selectedItem.DocumentItemInSeriesId == sn.DocumentItemInSeriesId);
      obj.IsChecked = (bool)!obj.IsChecked;
  }
  </code></pre>