0

There are similar topics here, but none of the resolutions have helped me.

I have the following:

<ControlTemplate x:Key="PlateWellControlTemplate"
                             TargetType="{x:Type ContentControl}">
                <Grid x:Name="PART_stateGrid"
                      Margin="0,0,5,0">
                    <Ellipse Fill="#FF252526"
                             MinWidth="34"
                             MinHeight="34"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center"/>
                    <Ellipse x:Name="PART_stateControl"
                             Fill="#FFE6E6E6"
                             MinWidth="32"
                             MinHeight="32"
                             HorizontalAlignment="Center"
                             VerticalAlignment="Center"/>
                    <Label FontWeight="Bold"
                           FontFamily="Verdana"
                           Foreground="Black"
                           Background="#00000000"
                           HorizontalAlignment="Center"
                           HorizontalContentAlignment="Center"
                           VerticalAlignment="Center"
                           Content=""/>
                </Grid>
            </ControlTemplate>

Used here:

<DataGridTemplateColumn Header="2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid Tag="{Binding WellX2}" 
                              x:Name="wellX2" 
                              MouseDown="well_Click">
                            <ContentControl Template="{DynamicResource PlateWellControlTemplate}"/>

Then accessed here in code-behind:

DataTemplate cellTemplate = (DataTemplate)cell.Template.FindName("PART_stateControl", cell);

Where "cell" is a non-null DataGridCell using the template. The above line always returns null and I do not understand why. I have also tried as a ControlTemplate and a ContentPresenter.

What I need is a reference to the Ellipse ("PART_StateControl") in the DataGridCell I've been handed as I need to change one of it's properties. But in general, how do I get to named items in the ContentControl Template? This is all triggered by a click event on the parent Grid control referenced in the DataGridTemplateColumn.CellTemplate here named "wellX2". Again, there are a few discussions on here regarding this, but none have helped. I feel like there's something silly missing. This has to be doable.

Thanks in advance for any help you can afford.

  • Just FYI, I already am aware that: DataTemplate cellTemplate = (DataTemplate)cell.Template.FindName("PART_stateControl", cell); is attempting to access the incorrect item name. This was only due to me trying several things and not correcting this one last item before I posted. What I'm really trying to get is: Ellipse ellipse = (Ellipse)cell.Template.FindName("PART_stateControl", cell); but this also returns null. It's not the cast that's an issue either. Even casting as "object" returns null. – Flacmonster Nov 02 '18 at 14:11
  • (Ellipse)cell.Template.FindName("PART_stateControl", cell); seems fishy. Mainly because I'm making a call on cell and handing it cell. – Flacmonster Nov 02 '18 at 14:18

1 Answers1

0

Answer found as a combination of this:

Getting a control from a DataGridCell

and this:

How to Get element inside the content control

Brilliant!