【问题标题】:how can i access the textbox inside the datagrid using the events of that datagrid?如何使用该数据网格的事件访问数据网格内的文本框?
【发布时间】:2013-04-14 22:09:33
【问题描述】:

我在使用 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>

【问题讨论】:

  • 如何使用datagrid1的事件访问文本框txtEmpid??????请帮帮我
  • with event 你是什么意思???在文本框中按下键,您想将其作为数据网格键按下吗?您可以查找路由事件
  • 每当用户点击网格时......即通过选定的索引更改事件我想将文本框中的文本作为 hiiiii

标签: wpf wpf-controls wpfdatagrid


【解决方案1】:

首先,您需要使用 ItemContainerGenerator 从数据中获取正确的行(在您的 datagrid1_SelectionChanged 事件中)。

var row = (DataGridRow)datagrid1.ItemContainerGenerator.
                     ContainerFromItem(datagrid1.SelectedItem);

然后像这样获取 TextBlock:

var text = datagrid1.Columns[0].GetCellContent(row) as TextBlock;

【讨论】:

  • 我无法让它工作 - 我得到了正确的行,GetCellContent 返回了一个 ContentPresenter,在 "as" 转换为 TextBox 之后,它变成了 null。我的 XAML 看起来非常接近原始问题中的那个。
【解决方案2】:
var cellInfo=dataGrid1.SelectedCells[0];
var txt=cellInfo.Column.GetCellContent(cellInfo.Item);

或者还有另一种解决方案来获取所有存在动态行的文本框的文本。

 DataRowView dataRow = (DataRowView)dataGrid1.SelectedItem;

int Columnindex = datagrid1.CurrentCell.Column.DisplayIndex;
 int iGridRowsCount      = ArgumentsDataGridforTestcasessTab.Items.Count;
for (int jRow = 0; jRow <= iGridRowsCount - 1; jRow++)
{

  DataGridCell cell = GetCell(jRow, Columnindex);
  ContentPresenter _contentPresenter = new ContentPresenter();
  _contentPresenter = (ContentPresenter)cell.Content;

  // get the attached control from the cell
  TextBox myTextBox = GetVisualChild<TextBox>(_contentPresenter);                 

}

【讨论】:

    【解决方案3】:

    注意单元格是处于编辑模式,还是仅处于预览模式。如果它处于编辑模式(我的意思是你正在写入它,或者你只是写了一些东西并且没有移动到其他单元格)它不会转换为 TextBlock,而是转换为 Textbox。

    当它(仅)处于预览模式时,它确实会转换为 TextBlock。

    我遇到了同样的问题,我在将内容投射到文本框时通过 try-catch 序列解决了这个问题。

    做过类似的事情

    try
    {
       var myTextboxVariable=(Textbox) datagridcell.Content
       //extract the text out of myVariable.Text, do whatever you intend to the Textbox
    }
    catch(Exception ex)
    {
      var myTextBlockVariable=(TextBlock) datagridcell.Content
      //extract the text out of myVariable.Text, do whatever you intend to the TextBlock
    }
    

    我知道它看起来像一个粗略的解决方案,并且必须有一个更优雅的方法。但最终重要的是结果。时间是我们维度的坐标:)

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 2017-05-27
      相关资源
      最近更新 更多