如何在WPF中选择单元格时选择数据网格单元格内的TextBox?

时间:2016-11-23 13:48:15

标签: c# wpf datagrid

我有一个数据网格,第一列的单元格用文本框覆盖。如果我用鼠标选择“单元格”没有问题,因为我只能单击TextBox,但是当我用键盘导航时,我可以导航到下面的单元格。我尝试设置单元格样式,但TextBox继承了设置。有没有办法在选择下面的单元格时将焦点移动到TextBox?

<DataGrid x:Name="buildDataGrid" 
        ItemsSource="{Binding BuildData}" 
        AutoGenerateColumns="False"
        CanUserReorderColumns="False" 
        CanUserSortColumns="False" 
        CanUserResizeRows="False" 
        SelectionUnit="CellOrRowHeader" 
        CanUserAddRows="False" 
        CanUserDeleteRows="False" 
        KeyboardNavigation.TabNavigation="None"
        Margin="0,0,10,0" IsReadOnly="True">
  <DataGrid.CellStyle>
      <Style TargetType="DataGridCell" >
          <Style.Setters>
              <Setter Property="IsEnabled" Value="True"/>
              <Setter Property="IsHitTestVisible" Value="False" />
          </Style.Setters>
      </Style>
  </DataGrid.CellStyle>
  <DataGrid.Columns>
      <DataGridTemplateColumn Header="Serial Number"  
                              MinWidth="200" 
                              Width="*" 
                              x:Name="componentSerialNumberDataGridTemplate">
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <TextBox Text="{Binding SerialNumber, UpdateSourceTrigger=PropertyChanged}" 
                           x:Name="snoTextBox" 
                           BorderThickness="0" 
                           Focusable="True" 
                           GotFocus="snoTextBox_GotFocus">
                      <TextBox.InputBindings>
                          <KeyBinding Command="{Binding SerialNumberEnterCommand}" 
                                      CommandParameter="{Binding Path=Text, ElementName=snoTextBox}" 
                                      Key="Return"/>
                      </TextBox.InputBindings>
                  </TextBox>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>

1 个答案:

答案 0 :(得分:0)

您需要创建强制文本框填充的行为(myTextBox.Focus())。 应在TextBox实例上分配此行为,并将其绑定到DataCell的属性IsFoucsed的实例。 这样的事情:

 <TextBox Text="{Binding SerialNumber, UpdateSourceTrigger=PropertyChanged}" 
                       x:Name="snoTextBox" 
                       BorderThickness="0" 
                       Focusable="True" 
                       local:Behaviour.ForceFoucusBoolean="{Binding IsFocused, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridCell}}}"
                       GotFocus="snoTextBox_GotFocus">
                  <TextBox.InputBindings>
                      <KeyBinding Command="{Binding SerialNumberEnterCommand}" 
                                  CommandParameter="{Binding Path=Text, ElementName=snoTextBox}" 
                                  Key="Return"/>
                  </TextBox.InputBindings>
              </TextBox>

你的行为是这样的:

 class Behaviour
{


    public static bool GetForceFoucusBoolean(DependencyObject obj)
    {
        return (bool)obj.GetValue(ForceFoucusBooleanProperty);
    }

    public static void SetForceFoucusBoolean(DependencyObject obj, bool value)
    {
        obj.SetValue(ForceFoucusBooleanProperty, value);
    }

    // Using a DependencyProperty as the backing store for ForceFoucusBoolean.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ForceFoucusBooleanProperty =
        DependencyProperty.RegisterAttached("ForceFoucusBoolean", typeof(bool), typeof(Behaviour), new PropertyMetadata(null,
            (o, e) =>
            {

                TextBox  tb = o as TextBox;
                if (tb != null)
                {
                    bool foucs = Convert.ToBoolean(e.NewValue);
                    if (selctAll)
                    {
                        tb.Foucs();
                    }
                    else
                    {
                        tb.Unfocus();
                    }
                }

            }));


}

那应该适合你。

相关问题