0

セル間のフォーカスナビゲーションは必要ありません。セルスタイルでFocusable="False"を設定し、行のfocusvisualstyleを調整しようとしましたが、その場合は選択に失敗します。

4

2 に答える 2

1

Selection Unitええ、DataGridをFullRowに設定borderThickness to 0し、で設定する必要がありますFocusVisualStyle to null

<DataGrid SelectionUnit="FullRow">
  <DataGrid.CellStyle>
      <Style TargetType="DataGridCell">
          <Setter Property="BorderThickness" Value="0"/>
          <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
       </Style>
  </DataGrid.CellStyle>
  <!-- ... -->
</DataGrid>

アップデート

上記のxamlは、xamlのみのアプローチで実行できる最善の方法ですが、集計も処理する場合は、コードビハインドに移動する必要があります。これが私がそれを達成した方法です-

<DataGrid x:Name="dg" ItemsSource="{Binding Objects}" SelectionUnit="FullRow">
  <DataGrid.CellStyle>
     <Style TargetType="DataGridCell">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <EventSetter Event="PreviewKeyDown" Handler="dg_PreviewKeyDown"/>
      </Style>
  </DataGrid.CellStyle>
</DataGrid>

コードビハインド(ここで私が行っているのは、ユーザーがキーを右または左に押して、あるセルから別のセルへのナビゲーションを停止するように処理する場合です。ユーザーがTabキーを押した場合、フォーカスは移動する代わりに次の行に移動する必要があります次のセルへ)-

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<DataGridRow>(sender as DependencyObject);
     DataGridRow nextDataGridRow =(DataGridRow)dg.ItemContainerGenerator
                                     .ContainerFromIndex(a.GetIndex() + 1);
     if (nextDataGridRow != null)
     {
        dg.SelectedIndex = a.GetIndex() + 1;
        DataGridCell cell = UtilityFunctions.FindChild<DataGridCell>
                              (nextDataGridRow as DependencyObject, "");
        cell.Focus();
     }
     e.Handled = true;
   }
}

上記のコードでは、ビジュアルツリーを移動してビジュアルツリーで必要な親または子を見つけるために必要ないくつかのユーティリティ関数を使用しました。参考までに、そのコードは次のとおりです-

public class UtilityFunctions
{
   public static Parent FindParent<Parent>(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<Parent>(parentObject);
            }
        }

   public static T FindChild<T>(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 < 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<T>(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 && 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;
        }
}
于 2012-08-16T10:18:22.363 に答える
0

セル間のフォーカスナビゲーションを削除するには:以下のコードを使用します

于 2012-08-16T07:40:47.563 に答える