【问题标题】:Focus to a specific editable datagrid cell in wpf DataGrid on RowSelection关注 RowSelection 上 wpf DataGrid 中的特定可编辑数据网格单元格
【发布时间】:2016-02-12 19:40:17
【问题描述】:

我在数据网格中有四列,第四列是模板化的,并且具有始终可编辑的文本框。

我在这里想要实现的是,当行选择更改突出显示的行的第四个单元格时,该单元格是可编辑的并且其中有文本框应该获得焦点。

我可以在代码隐藏或 xaml 中完成这项工作。

这是我所做的:

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

似乎没有任何效果。不知道是什么原因。谁能帮忙

【问题讨论】:

    标签: mvvm datagrid wpf-controls wpf-4.5


    【解决方案1】:

    声明

    txtB.Focus();
    

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

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

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

    【讨论】:

    • 这出乎意料:我创建了一个新的 WPF 应用程序,将您的代码放在 MainWindow.xaml 中,并用我的建议替换了语句 txtB.SelectAll()。单击单元格(在第 4 列中)设置行选择,将焦点放在编辑器单元格上,现在也选择了所有文本。也许我不明白你想要达到什么目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多