2016-06-30 5 views
0

DataGridDataGridTemplateColumn이 있습니다. 템플릿 열에는 ComboBox이 있습니다. 이 combobox에 초점을 맞추고 입력을 시작하여 ComboBox의 항목을 볼 수 있습니다. 그러나 이것은 예상보다 약간 까다 롭습니다.DataGridTemplate 콤보 상자에 포커스 설정

미리 감사드립니다.

XAML

 <DataGridTemplateColumn Header="ItemNumber"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox ItemsSource="{Binding DataContext.Parts, 
         RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
           x:Name="CboItems" 
           DisplayMemberPath="Code" 
           SelectedValuePath="Id" 
           SelectedValue="{Binding PartId}" 
           IsEditable="True" 
           SelectionChanged="ComboBox_SelectionChanged"> 
         <ComboBox.Style> 
          <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}"> 

          </Style> 
         </ComboBox.Style> 
        </ComboBox> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 

코드

private void SelectCorrectComboBox(ShilListItemArgs e) 
{ 
    DataGrid.ScrollIntoView(e.Item); 
    var rowIndex= DataGrid.SelectedIndex; 
    var row = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
    var cell = GetCell(DataGrid, row, 3) as DataGridCell; 

    if (cell!=null) 
    { 
     cell.IsEditing = true; 
     cell.IsSelected = true; 
     var contentpresenter = cell.Content as ContentPresenter; 
     if (contentpresenter != null) 
     { 
      contentpresenter.ApplyTemplate(); 
      var content = contentpresenter.ContentTemplate.FindName("CboItems",contentpresenter); 
      var comboBox = content as ComboBox; 
      if (comboBox != null) 
      { 
       comboBox.Focus(); 
      } 
     } 
    } 
} 

업데이트 나뿐만 아니라이 시도하고이 작업을 얻을 수 없습니다.

var edit = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo); 
+0

도움이 될만한 문제에 대한 몇 가지 세부 정보. – Ian

+0

그것은 ComboBox를 선택하지만 입력을 시작하면 시작하지 않습니다. 그것은 예외를 던지지 않습니다. 나는 단지 입력하지 않습니다. – JamTay317

답변

0

나는 anwser를 발견했다.

private void SelectCorrectComboBox(ShilListItemArgs e) 
    { 
     DataGrid.ScrollIntoView(e.Item); 
     var rowIndex= DataGrid.SelectedIndex; 
     var row = DataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; 
     var cell = GetCell(DataGrid, row, 3) as DataGridCell; 

     if (cell!=null) 
     { 
      cell.IsEditing = true; 
      cell.IsSelected = true; 
      var contentpresenter = cell.Content as ContentPresenter; 
      if (contentpresenter != null) 
      { 
       contentpresenter.ApplyTemplate(); 
       var content = contentpresenter.ContentTemplate.FindName("CboItems",contentpresenter); 
       var comboBox = content as ComboBox; 
       if (comboBox != null) 
       { 
        comboBox.Template.LoadContent(); 
        comboBox.SelectedIndex = 0; 
        comboBox.Focus(); 
        Task.Run(() => 
        { 
         Thread.Sleep(100); 
         var edit = (TextBox)comboBox.Template.FindName("PART_EditableTextBox", comboBox); 
         if (edit!=null) 
         { 
          Application.Current.Dispatcher.Invoke((Action) delegate 
          { 
           SetTextBox(edit); 
          }); 
         } 
        }); 
       } 
      } 
     } 
    } 

    void SetTextBox(TextBox t) 
    { 
     t.Focus(); 
    } 
관련 문제