Focus to a specific editable datagrid cell in wpf DataGrid on RowSelection

时间:2016-02-12 19:40:17

标签: mvvm datagrid wpf-controls wpf-4.5

I have four columns in a datagrid and fourth column is templated and has textbox which is always editable.

What i am trying to achieve here is when a row selection changes the highlighted row's fourth cell which is editable and has textbox in it should get focus.

I am ok to get this done in codebehind or xaml.

Here is what i did:

<DataGrid Name="MyGrid" SelectionChanged="MyGrid_OnSelectionChanged" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Str1}" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding Str2}" IsReadOnly="True"/>
            <DataGridTextColumn Binding="{Binding Str3}" IsReadOnly="True"/>
            <DataGridTemplateColumn >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Str4}" GotFocus="UIElement_OnGotFocus" >
                            <TextBox.Style >
                                <Style TargetType="TextBox">

                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridCell,AncestorLevel=1},Path=IsSelected}">
                                            <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

private void PopulateDummies()
    {
        int i = 0;
        List<SomeDummy> dummies = new List<SomeDummy>();
        while (i < 10)
        {
            dummies.Add(new SomeDummy
            {
                Str1 = string.Format("Str1:{0}", i),
                Str2 = string.Format("Str2:{0}", i),
                Str3 = string.Format("Str3:{0}", i),
                Str4 = string.Format("Str4:{0}", i)
            });
            i++;
        }
        MyGrid.ItemsSource = dummies;
    }

    private void MyGrid_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
    {
        var txtB = sender as TextBox;
        txtB.Focus();
        txtB.SelectAll();
    }

Nothing seems to work. Not sure what is the reason. Can anyone help

1 个答案:

答案 0 :(得分:0)

声明

txtB.Focus();

通过TextEditory控件有效地替换DataGridCell。为此,需要更新视图,因此需要运行UI-tread。 SelectAll在创建编辑器之前无法运行,因此将其作为单独的Dispatcher-job运行并将其替换为以下内容:

Application.Current.Dispatcher.BeginInvoke(new Action(() => { 
    txtB.SelectAll();
}), DispatcherPriority.Render);

这样就可以解决问题,你不需要为此目的使用SelctionChanged事件处理程序。