在ControlTemplate中声明子控件的属性后,如何访问它? [英] How to access a child control's property when it is declared in a ControlTemplate?
本文介绍了在ControlTemplate中声明子控件的属性后,如何访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个GridView,我想在其中将一个复选框嵌入到其中一列的标题中.我需要在代码隐藏中检查它的IsChecked属性.但是,我无法按名称访问它,因为它位于列标题的模板中,如下所示:
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>
...
是否还有另一种方法可以在代码隐藏中访问IsChecked属性的值?还是我一开始会以错误的方式解决这个问题?
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?
推荐答案
您需要将其绑定到某些属性,以这种方式访问要容易得多.
You'll want to bind it to some property, it's far easier to access that way.
这篇关于在ControlTemplate中声明子控件的属性后,如何访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文