将焦点设置在DataGridTemplate组合框上

时间:2016-06-30 15:05:56

标签: c# wpf combobox datagridtemplatecolumn

我的DataGrid有一个DataGridTemplateColumn。在模板列中,我有一个ComboBox。我想将重点放在combobox上,并能够开始输入以查看ComboBox中的项目。然而,这比预期的要复杂一些。

提前感谢。

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

1 个答案:

答案 0 :(得分: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();
    }