下降时DataGridCell是一个TextBlock,而上升时是TextBox

时间:2022-10-03 21:16:18

So, I have a DataGrid, which contains elements that look like this:

所以,我有一个DataGrid,其中包含如下所示的元素:

<DataGridTextColumn Header="Dto 1" Binding="{Binding Path=Dto1}">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <EventSetter Event="PreviewKeyDown" Handler="TextBox_PreviewKeyDown"/>
            <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

The PreviewKeyDown works perfectly fine, when I go up, I am able to get back to the cell with no problem. The way I see it, for all intents and purposes, I got a TextBox in there.

PreviewKeyDown工作得非常好,当我上升时,我能够毫无问题地回到单元格。我看到它的方式,无论出于什么意图和目的,我都有一个TextBox。

Now, when I try to go down from the cell in an event:

现在,当我试图在一个事件中从单元格下来时:

private void dgPropuestas_GotFocus(object sender, RoutedEventArgs e) {
        var cell = e.OriginalSource as DataGridCell;
        if (cell != null) {
            var cp = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(cell, 0), 0) as ContentPresenter;
            var tb = cp.Content as TextBlock;
            if (tb != null)
                tb.Focus();
        }
    }

(Note: I put the VisualTreeHelper method twice manually, I do have the FindVisualChildren one, but I went through the tree manually and as an act of desperation to speed up a bit, I put it manually)

(注意:我手动将VisualTreeHelper方法放两次,我有一个FindVisualChildren方法,但是我手动浏览了树,并且作为一种绝望的行为加速了一点,我手动把它放了)

If I try to declare tb as cp.Content as TextBox I get a null. For some reason, the ContentPresenter has inside a TextBlock, not a TextBox. FindVisualChildren(cell) returns an empty IEnumerable.

如果我尝试将tb声明为cp.Content作为TextBox,我得到一个null。出于某种原因,ContentPresenter内部有TextBlock,而不是TextBox。 FindVisualChildren(cell)返回一个空的IEnumerable。

Why is this a problem? Because I need to be able to call SelectAll() on the text, and textBlock doesn't offer that option. Any help is greatly appreciated. Thanks in advance!

为什么这是个问题?因为我需要能够在文本上调用SelectAll(),而textBlock不提供该选项。任何帮助是极大的赞赏。提前致谢!

Later Edit: Apparently, when not in edit mode, the datagrid contains a textblock. When in edit mode, a textbox. Now, datagrid.BeginEdit() doesn't seem to work, as it doesn't initialize the TextBox nor does it trigger the event of PrepareCellForEditing.

稍后编辑:显然,当不处于编辑模式时,datagrid包含一个文本块。在编辑模式下,文本框。现在,datagrid.BeginEdit()似乎不起作用,因为它没有初始化TextBox,也没有触发PrepareCellForEditing事件。

2 个解决方案

#1


0  

Totally approaching this the wrong way. You subscribe to events on a TextBox, so the sender will always be said TextBox.

完全接近这个错误的方式。您订阅了TextBox上的事件,因此始终会将发件人称为TextBox。

#2


0  

Given that the TextBox would not get initialized with BeginEditing() nor with isEditing = true, the problem was fixed by declaring the fields as textbox and forcing them upon the datagrid:

鉴于TextBox不会使用BeginEditing()或isEditing = true进行初始化,因此通过将字段声明为文本框并在数据网格上强制它们来修复问题:

<DataGridTemplateColumn Header="Dto 2">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Dto2}" GotFocus="TextBox_GotFocus" PreviewKeyDown="TextBox_PreviewKeyDown"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

#1


0  

Totally approaching this the wrong way. You subscribe to events on a TextBox, so the sender will always be said TextBox.

完全接近这个错误的方式。您订阅了TextBox上的事件,因此始终会将发件人称为TextBox。

#2


0  

Given that the TextBox would not get initialized with BeginEditing() nor with isEditing = true, the problem was fixed by declaring the fields as textbox and forcing them upon the datagrid:

鉴于TextBox不会使用BeginEditing()或isEditing = true进行初始化,因此通过将字段声明为文本框并在数据网格上强制它们来修复问题:

<DataGridTemplateColumn Header="Dto 2">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Dto2}" GotFocus="TextBox_GotFocus" PreviewKeyDown="TextBox_PreviewKeyDown"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>