在DataGridCell的CellEditingTemplate寻找一个TextBox [英] Finding a TextBox in a DataGridCell's CellEditingTemplate

查看:195
本文介绍了在DataGridCell的CellEditingTemplate寻找一个TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF的问题编程访问在DataGridTemplateColumn.CellEditingTemplate一个文本框,当细胞被选中,并在编辑模式。

I have a problem in WPF of programatically accessing a textbox in a DataGridTemplateColumn.CellEditingTemplate, when the cell is selected and in editing mode.

下面是的XAML我的DataGrid:

Here is the XAML of my DataGrid:

<DataGrid x:Name="OrderLinesGrid"
          Style="{StaticResource DataGridStyle}"
          SelectionMode="Single"
          SelectionUnit="Cell"
          ItemsSource="{Binding OrderLines}">
  <DataGrid.Columns>
    <DataGridTemplateColumn x:Name="NumberColumn"
                            Header="Varenr."
                            MinWidth="100">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding Number}" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
      <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
          <TextBox Text="{Binding Number}" />
        </DataTemplate>
      </DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>



我如何访问文本框时,选定的单元格?下面是该数据网格的可视化树一个形象,如果能够帮助你:

How can I access that TextBox when the cell is selected? Here is a image showing the visual tree of the DataGrid if that can help you:

我已经试过了DataGridCell GotFocus事件以下,但没有运气。它只是因为它是没有找到返回NULL。

I have tried the following in a DataGridCell GotFocus event, but without luck. It simply returns NULL because it is not found.

private void DataGridCellGotFocus(object sender, RoutedEventArgs e)
{
    var cell = sender as DataGridCell;
    var textBox = FindChild<TextBox>(cell, null);
}



当FindChild方法如下:

Where the FindChild method is the following:

/// <summary>
/// Finds a Child of a given item in the visual tree. 
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.</returns>
public static T FindChild<T>(DependencyObject parent, string childName)
    where T : DependencyObject
{
    // Confirm parent and childName are valid. 
    if (parent == null) return null;

    T foundChild = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        T childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild<T>(child, childName);

            // If the child is found, break so we do not overwrite the found child. 
            if (foundChild != null) break;
        }
        else if (!string.IsNullOrEmpty(childName))
        {
            var frameworkElement = child as FrameworkElement;
            // If the child's name is set for search
            if (frameworkElement != null && frameworkElement.Name == childName)
            {
                // if the child's name is of the request name
                foundChild = (T)child;
                break;
            }
        }
        else
        {
            // child element found.
            foundChild = (T)child;
            break;
        }
    }

    return foundChild;
}



我怀疑它做的DataTemplate中,但我对需要一些建议如何选择文本框的子元素?

I suspect it has something to do with the DataTemplate but I need some suggestions on how to select the TextBox child element?

推荐答案

我觉得你应该避免使用 VisualTreeHelper 尽可能。如果我的理解,你可以封装在 CellEditingCommand

I think you you should avoid using VisualTreeHelper as much as possible. If i understood, you can encapsulate your login within CellEditingCommand

<DataGridTemplateColumn.CellEditingTemplate>
  <DataTemplate>
    <TextBox Text="{Binding Number}">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="GotFocus">
          <i:InvokeCommandAction Command="{Binding CellEditingCommand}" />
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </TextBox>
  </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>



您也可以使用的行为

UPD

<DataTemplate>
    <TextBox Text="{Binding Number}">
        <Interactivity:Interaction.Triggers>
            <Interactivity:EventTrigger EventName="Loaded">
                <TriggerActions:TakeFocusAction />
            </Interactivity:EventTrigger>
        </Interactivity:Interaction.Triggers>
    </TextBox>
</DataTemplate>

和触发动作

public class TakeFocusAction : TriggerAction<UIElement>
{
    protected override void Invoke(object parameter)
    {
        AssociatedObject.Focus();
    }
}

这篇关于在DataGridCell的CellEditingTemplate寻找一个TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆