2014-07-14 277 views
0

我有一個DataGrid,當用戶點擊向下箭頭時,它需要移動到下面的下一行,但重點放在有文本框的單元格中。我不想關注文本框不是實際的單元格。這就是我的DataGrid的外觀。WPF DataGrid將行選擇更改爲單元格的文本框

<DataGrid Name="myDataGrid" PreviewKeyDown="myDataGrid_PreviewKeyDown"> 
<DataGrid.Columns> 
    <DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="Auto"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
        <TextBlock HorizontalAlignment="Right" Text="{Binding Path=Descrip, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, StringFormat=C, ConverterCulture=en-us}" TextAlignment="Right"/> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn Header="Units Counted" Width="Auto"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBox BorderBrush="Black" BorderThickness="2" FontWeight="Bold" Margin="2" Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 

private void myDataGrid_PreviewKeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Down) 
     { 
      e.Handled = true; 
      int indx = myDataGrid.SelectedIndex + 1; 
      myDataGrid.SelectedIndex = indx; 
      myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[1]) 
     } 
    } 

這樣做選擇該小區不是文本框。如果我將它添加到文本框;

FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" 

它爲每行文本框添加一個光標,但不關注文本框。

謝謝

編輯:謝謝@Sheridan我更接近這個代碼;

<TextBox 
BorderBrush="Black" 
BorderThickness="2" 
FontWeight="Bold" 
Margin="2" 
Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
TextAlignment="Right" 
FocusManager.IsFocusScope="True" 
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/> 

然後在我的myDataGrid_PreviewKeyDown將在myDataGrid.BeginEdit()允許爲它工作。問題是,似乎BeginEdit從來沒有真正結束。我該如何解決?

+1

我建議您閱讀MSDN上的[Focus Overview](http://msdn.microsoft.com/en-us/library/aa969768(v = vs.110).aspx)頁面。 *它將光標添加到每一行文本框,但不會將焦點放在文本框上*設置「FocusManager.FocusedElement」屬性將*邏輯焦點*設置爲其焦點範圍內的一個元素* ......所以它們*專注,但只是*在他們的焦點範圍內*。請閱讀鏈接頁面,瞭解如何管理您的重點需求。 – Sheridan

+0

請參閱上面的修改 – Xaphann

回答

1

我不會搞砸FocusManager.FocusedElement,因爲你會得到未知的警告,它可以像你現在正在經歷的BeginEdit一樣。

我所做的專注於TextBox很簡單,在XAML

<DataGrid Name="myDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Loans}" PreviewKeyDown="UIElement_OnPreviewMouseDown"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Units Counted" Width="Auto"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox x:Name="sampleTextBox" HorizontalAlignment="Right" Text="{Binding Name}"/> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

,並在類文件中我有

public static T GetVisualChild<T>(Visual parent) where T : Visual 
    { 
     T child = default(T); 
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
     for (int i = 0; i < numVisuals; i++) 
     { 
      Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
      child = v as T; 
      if (child == null) 
      { 
       child = GetVisualChild<T>(v); 
      } 
      if (child != null) 
      { 
       break; 
      } 
     } 
     return child; 
    } 

    private void UIElement_OnPreviewMouseDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key != Key.Down) return; 
     e.Handled = true; 
     var indx = myDataGrid.SelectedIndex + 1; 
     myDataGrid.SelectedIndex = indx; 
     myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[0]); 

     var row = this.myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item) as DataGridRow; 
     var presenter = GetVisualChild<DataGridCellsPresenter>(row); 


     var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item); 
     if (cell != null) 
     { 
      var contentPresenter = cell.Content as ContentPresenter; 
      if (contentPresenter != null) 
      { 
       var m = contentPresenter.ContentTemplate.FindName("sampleTextBox", contentPresenter); 
       ((TextBox) m).Focus(); 
      } 
     } 
    } 
} 

現在會做TextBox成爲焦點,當Down關鍵是每次按下並更改單元格選擇。

相關問題