如何在 ControlTemplate 中声明子控件的属性时访问它? [英] How to access a child control's property when it is declared in a ControlTemplate?

查看:21
本文介绍了如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆