K J
K J

Reputation: 612

How to access a child control's property when it is declared in a ControlTemplate?

I have a GridView in which I want to embed a checkbox into the header of one of the columns. I need to check it's IsChecked property in code-behind. However, I cannot access it by name as it is in a template for the column header like below:

<ListView.View>
    <GridView>
        <GridViewColumn>
            <GridViewColumn.HeaderContainerStyle>
                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}" >
                                <Border Background="SkyBlue" BorderBrush="Black" BorderThickness="1,1,0,1">
                                <CheckBox Name="_cbAllSettingFiles" 
                                          Command="{Binding Path=CheckAllLocationFilters}" 
                                          CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GridViewColumn.HeaderContainerStyle>
...

Is there another way to access the value of the IsChecked property in code-behind? Or am I going about this the wrong way in the first place?

Upvotes: 1

Views: 192

Answers (1)

brunnerh
brunnerh

Reputation: 185300

You'll want to bind it to some property, it's far easier to access that way.

Upvotes: 1

Related Questions