2016-06-30 13 views
0

DataGridにはDataGridTemplateColumnがあります。テンプレート欄には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(); 
    } 
関連する問題