Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataGridCell中查找文本框';s细胞模板_C#_Wpf_Xaml - Fatal编程技术网

C# 在DataGridCell中查找文本框';s细胞模板

C# 在DataGridCell中查找文本框';s细胞模板,c#,wpf,xaml,C#,Wpf,Xaml,我在WPF中遇到一个问题,即当选定单元格并处于编辑模式时,以编程方式访问DataGridTemplateColumn.CellEditingTemplate中的文本框 以下是我的DataGrid的XAML: <DataGrid x:Name="OrderLinesGrid" Style="{StaticResource DataGridStyle}" SelectionMode="Single" SelectionUnit="Ce

我在WPF中遇到一个问题,即当选定单元格并处于编辑模式时,以编程方式访问DataGridTemplateColumn.CellEditingTemplate中的文本框

以下是我的DataGrid的XAML:

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

当选定单元格时,如何访问该文本框?下面是一幅显示DataGrid可视树的图像,如果这有助于您:

我在DataGridCell GotFocus事件中尝试了以下内容,但没有成功。它只是返回NULL,因为找不到它

private void DataGridCellGotFocus(object sender, RoutedEventArgs e)
{
    var cell = sender as DataGridCell;
    var textBox = FindChild<TextBox>(cell, null);
}
private void DataGridCellGotFocus(对象发送方,RoutedEventTargets e)
{
var cell=发送方作为DataGridCell;
var textBox=FindChild(单元格,空);
}
其中FindChild方法如下所示:

/// <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;
}
//
///在可视树中查找给定项的子项。
/// 
///查询项的直接父项。
///查询项的类型。
///x:孩子的名字或名字。
///与提交的类型参数匹配的第一个父项。
///如果找不到匹配的项目,
///正在返回空的父级。
公共静态T FindChild(DependencyObject父对象,字符串childName)
其中T:DependencyObject
{
//确认父项和子项名称有效。
if(parent==null)返回null;
T foundChild=null;
int childrenCount=visualtreeheloper.GetChildrenCount(父级);
for(int i=0;i

我怀疑这与DataTemplate有关,但我需要一些关于如何选择TextBox子元素的建议。

我认为您应该尽量避免使用
VisualTreeHelper
。如果我理解,您可以将登录名封装在
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();
    }
}
公共类TakeFocusAction:TriggerAction
{
受保护的覆盖无效调用(对象参数)
{
AssociatedObject.Focus();
}
}

我认为您应该尽量避免使用
VisualTreeHelper
。如果我理解,您可以将登录名封装在
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();
    }
}
公共类TakeFocusAction:TriggerAction
{
受保护的覆盖无效调用(对象参数)
{
AssociatedObject.Focus();
}
}

我认为你应该处理
PreparingCellForEdit
:Sth

void MainDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
   TextBox tb = e.Column.GetCellContent(e.Row) as TextBox;
}

请看:

我认为你应该处理
PreparingCellForEdit
:Sth

void MainDataGrid_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
   TextBox tb = e.Column.GetCellContent(e.Row) as TextBox;
}


请参阅:

您想用该编辑器做什么?你的目标是什么?首先,我需要在单击DataGridCell时将焦点放在文本框(和其他控件)上。你想用这个编辑器做什么?你的目标是什么?首先,我需要在单击DataGridCell时为文本框(和其他控件)设置焦点。我的问题是,当单元格接收焦点(即单击)时,我需要以编程方式选择文本框。你可以为此编写行为。你的解决方案在文本框接收焦点时触发,而不是DataGridCell。接收焦点的是DataGridCell,在DataGridCell的GotFocus事件处理程序中,我需要以编程方式选择文本框。如果您只是想在单元格开始编辑时将焦点设置在文本框上,请参阅UPDBeautiful解决我的焦点问题的方法。非常感谢你!但是,我仍然想知道如何通过编程访问该文本框。但这解决了我的一个问题。我的问题是,当单元格接收焦点(即单击)时,我需要以编程方式选择文本框。你可以为此编写行为。当文本框接收焦点时,你的解决方案触发,而不是DataGridCell。接收焦点的是DataGridCell,在DataGridCell的GotFocus事件处理程序中,我需要以编程方式选择文本框。如果您只是想在单元格开始编辑时将焦点设置在文本框上,请参阅UPDBeautiful解决我的焦点问题的方法。非常感谢你!但是,我仍然想知道如何通过编程访问该文本框。但这解决了我的一个问题。啊哈,我不知道有一个PreparingCellForEdit事件。但是e.Column.GetCellContent(e.Row)返回的是ContentPresenter,而不是文本框。所以这就给我留下了同样的问题。什么是
ContentPresenter.Content
?我的意思是,如果搜索该
ContentPresenter
的VisualTree,它将返回null。我相信这与我在CellEdit中使用的数据模板有关