我有一个包含四列的数据网格。
所以从C#端的这个数据网格,我想找到名称=“headBox”的特定文本框
我是怎么做的?我的列的xaml如下所示。此部分位于Datagrid.Columns中,该数据位于datagrid标记内,其名称为= dgrid
<DataGridTemplateColumn Header="Header">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="HeadBox"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
答案 0 :(得分:3)
我认为你可以使用
DataGridRow row = myDataGrid.ItemContainerGenerator.ContainerFromIndex
(myDataGrid.SelectedIndex) as DataGridRow;
var i = 0; /// Specify your column index here.
修改强>
TextBox ele = ((ContentPresenter)(myDataGrid.Columns[i].GetCellContent(row))).Content as TextBox;
答案 1 :(得分:0)
<DataGridTemplateColumn Header="ClientDomain" Width="130" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="directionColumnTextBlock" Text="{Binding Path=ClientDomain,Mode=TwoWay}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IssReadOnly}" Value="true">
<Setter TargetName="directionColumnTextBlock" Property="IsReadOnly" Value="true" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex
(dataGrid.SelectedIndex) as DataGridRow;
TextBox clientDomain= ((ContentPresenter)(dataGrid.Columns[2].GetCellContent(row))).Content as TextBox