-2

我在使用 xaml 设计的数据网格中有一个文本框。我可以使用数据网格的事件访问以前在代码文件中在 xaml 中设计的文本框吗?请帮我.....................

<Window x:Class="GridTextBox.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" WindowState="Maximized"
    Title="Test" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".25*"/>

        <ColumnDefinition Width=".25*"/>
        <ColumnDefinition Width=".25*"/>
        <ColumnDefinition Width=".25*"/>
    </Grid.ColumnDefinitions>
    <DataGrid Grid.Row="1" Grid.Column="1"  Name="datagrid1" SelectionChanged="datagrid1_SelectionChanged" LoadingRowDetails="DataGrid_LoadingRowDetails"  Height="auto" Width="auto">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Name="txtEmpid" Text="hiiiiii"></TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

4

2 回答 2

0

您可以为 DataGrid 使用以下扩展。
只需创建一个新模块并插入以下代码。

Module Extensions
<Extension()>
  Public Function GetDataTemplateObjectFromSelectedRow(Of DataTemplateType As Visual)(ByVal lDataGrid As DataGrid, ByVal lColumnIndex As Integer) As DataTemplateType
    Dim lRow As DataGridRow
    Dim lCell As DataGridCell
    Dim lPresenter As DataGridCellsPresenter
    Dim lFoundObject As DataTemplateType = Nothing

    If lDataGrid.SelectedIndex >= 0 Then
      lRow = lDataGrid.ItemContainerGenerator.ContainerFromIndex(lDataGrid.SelectedIndex)
      lPresenter = GetVisualChild(Of DataGridCellsPresenter)(lRow)
      lCell = lPresenter.ItemContainerGenerator.ContainerFromIndex(lColumnIndex)
      lFoundObject = GetVisualChild(Of DataTemplateType)(lCell)
    End If
    Return lFoundObject
  End Function

  Public Function GetVisualChild(Of T As Visual)(parent As Visual) As T
    Dim child As T = Nothing
    Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)
    For i As Integer = 0 To numVisuals - 1
      Dim v As Visual = DirectCast(VisualTreeHelper.GetChild(parent, i), Visual)
      child = TryCast(v, T)
      If child Is Nothing Then
        child = GetVisualChild(Of T)(v)
      End If
      If child IsNot Nothing Then
        Exit For
      End If
    Next
    Return child
  End Function

End Module



之后,您可以在 DataGrid 的事件中调用以下函数:

  Private Sub DataGridButtons_CurrentCellChanged(sender As Object, e As EventArgs) Handles DataGridButtons.CurrentCellChanged
    Dim lTextBox As TextBox = DataGridButtons.GetDataTemplateObjectFromSelectedRow(Of TextBox)(0)
  End Sub

该部分(Of TextBox)描述了您预期的 DataType 并使用(0)您描述相关的参数GridColumn

于 2013-04-15T10:30:16.153 回答
0

C#的解决方案

namespace ExtensionMethods
{
  public static class Extensions
  {

    public static DataTemplateType GetDataTemplateObjectFromSelectedRow<DataTemplateType>(DataGrid lDataGrid, int lColumnIndex) where DataTemplateType : Visual
    {
      DataGridRow lRow = default(DataGridRow);
      DataGridCell lCell = default(DataGridCell);
      DataGridCellsPresenter lPresenter = default(DataGridCellsPresenter);
      DataTemplateType lFoundObject = null;

      if (lDataGrid.SelectedIndex >= 0)
      {

        lRow = (DataGridRow)lDataGrid.ItemContainerGenerator.ContainerFromIndex(lDataGrid.SelectedIndex);
        lPresenter = GetVisualChild<DataGridCellsPresenter>(lRow);
        lCell = (DataGridCell)lPresenter.ItemContainerGenerator.ContainerFromIndex(lColumnIndex);
        lFoundObject = GetVisualChild<DataTemplateType>(lCell);
      }
      return lFoundObject;
    }

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
      T child = null;
      int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
      for (int i = 0; i <= numVisuals - 1; i++)
      {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
          child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
          break; // TODO: might not be correct. Was : Exit For
        }
      }
      return child;
    }

  }
}

使用以下调用获取文本框:

private void DataGridButtons_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  TextBox lTextBox = Extensions.GetDataTemplateObjectFromSelectedRow<TextBox>(DataGridButtons, 0);
}
于 2013-04-15T12:19:58.730 回答