Reputation: 126
I override my DataGrid to use RichTextBox as DataGridTemplateColumn. Now in code behind I want to have access to these RichTexBoxes from the grid. For example I want to get the RichTextBox of row 10 column 1. How can I do that?
<DataGridTemplateColumn Header="First Language" IsReadOnly="False" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
RichTextBox>
<FlowDocument IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
<Paragraph FontFamily="Segoe UI" FontSize="14 ">
<Run Text="{Binding Path=First ,Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}" />
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
UPDATE:
with the code I find below I was able to get DataGridCell, but casting the the content as RichTextBox gives me null. Any ideas?
https://svgvijay.blogspot.de/2013/01/how-to-get-datagrid-cell-in-wpf.html
Upvotes: 0
Views: 456
Reputation: 169200
Try this:
int row = 0;
int column = 0;
DataGridCell cell = Datagrid.GetCell(dataGrid, row, column);
ContentPresenter cp = cell.Content as ContentPresenter;
if (VisualTreeHelper.GetChildrenCount(cp) > 0)
{
RichTextBox rtb = VisualTreeHelper.GetChild(cp, 0) as RichTextBox;
}
Upvotes: 1