Getting a control from a DataGridCell

You can use the name of the control to find it in the template, e.g.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <uc:Bogus x:Name="root" ItemsSource="{Binding Machines}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
var cell = dataGrid.GetCell(5, 0);
var cp = (ContentPresenter)cell.Content;
var bogus = (Bogus)cp.ContentTemplate.FindName("root", cp);

Note however that this usually should not be necessary as modifying templated controls for the most part can be done using data binding, attached properties and events alone. In general i would restrict template access via code to custom controls (which often have designated parts).

Tags:

C#

Wpf

Datagrid