问题
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