Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yeah you need to set the <code>Selection Unit</code> for DataGrid to <code>FullRow</code> and set <code>borderThickness to 0</code> with <code>FocusVisualStyle to null</code>.</p> <pre><code>&lt;DataGrid SelectionUnit="FullRow"&gt; &lt;DataGrid.CellStyle&gt; &lt;Style TargetType="DataGridCell"&gt; &lt;Setter Property="BorderThickness" Value="0"/&gt; &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt; &lt;/Style&gt; &lt;/DataGrid.CellStyle&gt; &lt;!-- ... --&gt; &lt;/DataGrid&gt; </code></pre> <p><strong>UPDATE</strong></p> <p>Above stated xaml is best you can do with xaml only approach but in case you want to handle the tabulation too, then you have to go to code behind. This is how i achieved it -</p> <pre><code>&lt;DataGrid x:Name="dg" ItemsSource="{Binding Objects}" SelectionUnit="FullRow"&gt; &lt;DataGrid.CellStyle&gt; &lt;Style TargetType="DataGridCell"&gt; &lt;Setter Property="BorderThickness" Value="0"/&gt; &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt; &lt;EventSetter Event="PreviewKeyDown" Handler="dg_PreviewKeyDown"/&gt; &lt;/Style&gt; &lt;/DataGrid.CellStyle&gt; &lt;/DataGrid&gt; </code></pre> <p>Code behind (What i am doing here is if user pressed key right or left simply handle them so as to stop the navigation from one cell to other and in case user press Tab key, focus should go to the next row if available instead of moving to next cell) -</p> <pre><code>private void dg_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Left || e.Key == Key.Right) e.Handled = true; else if (e.Key == Key.Tab) { DataGridRow a = UtilityFunctions.FindParent&lt;DataGridRow&gt;(sender as DependencyObject); DataGridRow nextDataGridRow =(DataGridRow)dg.ItemContainerGenerator .ContainerFromIndex(a.GetIndex() + 1); if (nextDataGridRow != null) { dg.SelectedIndex = a.GetIndex() + 1; DataGridCell cell = UtilityFunctions.FindChild&lt;DataGridCell&gt; (nextDataGridRow as DependencyObject, ""); cell.Focus(); } e.Handled = true; } } </code></pre> <p>In the above code i have used some utility functions required to travel the Visual tree to find necessary parent or child in the Visual tree. For your reference the code for it as follows - </p> <pre><code>public class UtilityFunctions { public static Parent FindParent&lt;Parent&gt;(DependencyObject child) where Parent : DependencyObject { DependencyObject parentObject = child; //We are not dealing with Visual, so either we need to fnd parent or //get Visual to get parent from Parent Heirarchy. while (!((parentObject is System.Windows.Media.Visual) || (parentObject is System.Windows.Media.Media3D.Visual3D))) { if (parentObject is Parent || parentObject == null) { return parentObject as Parent; } else { parentObject = (parentObject as FrameworkContentElement).Parent; } } //We have not found parent yet , and we have now visual to work with. parentObject = VisualTreeHelper.GetParent(parentObject); //check if the parent matches the type we're looking for if (parentObject is Parent || parentObject == null) { return parentObject as Parent; } else { //use recursion to proceed with next level return FindParent&lt;Parent&gt;(parentObject); } } public static T FindChild&lt;T&gt;(DependencyObject parent, string childName) where T : DependencyObject { // Confirm parent is valid. if (parent == null) return null; T foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i &lt; childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); // If the child is not of the request child type child T childType = child as T; if (childType == null) { // recursively drill down the tree foundChild = FindChild&lt;T&gt;(child, childName); // If the child is found, break so we do not overwrite the found child. if (foundChild != null) break; } else if (!string.IsNullOrEmpty(childName)) { var frameworkElement = child as FrameworkElement; // If the child's name is set for search if (frameworkElement != null &amp;&amp; frameworkElement.Name == childName) { // if the child's name is of the request name foundChild = (T)child; break; } } else { // child element found. foundChild = (T)child; break; } } return foundChild; } } </code></pre>
 

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