Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've tried these suggestions, and plenty of others I've found on other sites, but none of them quite worked for me. In the end, I created the following solution.</p> <p>I've created my own DataGrid-inherited control, and simply added this code to it:</p> <pre><code>public class DataGridWithNavigation : Microsoft.Windows.Controls.DataGrid { public DataGridWithNavigation() { EventManager.RegisterClassHandler(typeof(DataGridCell), DataGridCell.PreviewMouseLeftButtonDownEvent, new RoutedEventHandler(this.OnPreviewMouseLeftButtonDown)); } private void OnPreviewMouseLeftButtonDown(object sender, RoutedEventArgs e) { DataGridCell cell = sender as DataGridCell; if (cell != null &amp;&amp; !cell.IsEditing &amp;&amp; !cell.IsReadOnly) { DependencyObject obj = FindFirstControlInChildren(cell, "CheckBox"); if (obj != null) { System.Windows.Controls.CheckBox cb = (System.Windows.Controls.CheckBox)obj; cb.Focus(); cb.IsChecked = !cb.IsChecked; } } } public DependencyObject FindFirstControlInChildren(DependencyObject obj, string controlType) { if (obj == null) return null; // Get a list of all occurrences of a particular type of control (eg "CheckBox") IEnumerable&lt;DependencyObject&gt; ctrls = FindInVisualTreeDown(obj, controlType); if (ctrls.Count() == 0) return null; return ctrls.First(); } public IEnumerable&lt;DependencyObject&gt; FindInVisualTreeDown(DependencyObject obj, string type) { if (obj != null) { if (obj.GetType().ToString().EndsWith(type)) { yield return obj; } for (var i = 0; i &lt; VisualTreeHelper.GetChildrenCount(obj); i++) { foreach (var child in FindInVisualTreeDown(VisualTreeHelper.GetChild(obj, i), type)) { if (child != null) { yield return child; } } } } yield break; } } </code></pre> <p>What does all this do ?</p> <p>Well, each time we click on any cell in our DataGrid, we see if the cell contains a CheckBox control within it. If it <em>does</em>, then we'll set the focus to that CheckBox <em>and toggle it's value</em>.</p> <p>This seems to work for me, and is a nice, easily reusable solution.</p> <p>It is disappointing that we <em>need</em> to write code to do this though. The explanation that the first mouse click (on a DataGrid's CheckBox) is "ignored" as WPF uses it to put the row into Edit mode might sound logical, but in the real-world, this goes against the way every real application works.</p> <p>If a user sees a checkbox on their screen, they should be able to click on it once to tick/untick it. End of story.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload