Ad

How To Access A Child Control's Property When It Is Declared In A ControlTemplate?

- 1 answer

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?

Ad

Answer

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

Ad
source: stackoverflow.com
Ad