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

自作多情 提交于 2021-02-16 15:20:23

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/10526017/how-to-access-a-child-controls-property-when-it-is-declared-in-a-controltemplat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!