2016-11-23 19 views
0

私は最初の列のセルがテキストボックスでオーバーレイされているデータグリッドを持っています。私がTextBoxをクリックするだけで問題ないマウスで「セル」を選択すると、キーボードでナビゲートすると、下のセルに移動できます。私はセルスタイルを設定しようとしましたが、TextBoxは設定を継承します。下のセルが選択されているときにTextBoxにフォーカスを移動する方法はありますか?WPFでセルが選択されているときに、DataGridセル内の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> 

答えて

0

テキストボックスを強制的に作成する動作(myTextBox.Focus())を作成する必要があります。 この動作は、TextBoxインスタンスに割り当てられ、DataCellのプロパティIsFoucsedのインスタンスにバインドされる必要があります。そのような 何か:

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(); 
        } 
       } 

      })); 


} 

あなたのために働く必要があります。

<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> 

あなたの行動は、そのようなものです。

+0

ありがとう@ Mr.Bさん、私は試しました。残念ながら、 'Default value typeは 'ForceFoucusBoolean'プロパティの型とマッチしません。 – Steven

関連する問題