在代码中访问DataGridHeader模板

问题描述:

您好,专家,

我有一个Datagrid和一个headerTemplate(一个复选框).如何在代码中访问此CheckBox.我尝试了DataGrid.Find方法,但是它不起作用.我还尝试了DataGrid.Columns [0] .Header;.并且也返回null.如何访问代码中的复选框?

我的代码:

Hi Experts,

I have a Datagrid and a headerTemplate(a check Box). How can I access this CheckBox in Code. I tried the DataGrid.Find method, but it is not working. I also Tried the DataGrid.Columns[0].Header; and that too returns null. How I can access the checkbox in code?

My Code :

<usercontrol.resources>
<Style x:Key="DataGridColumnHeaderStyle" TargetType="dataPrimitives:DataGridColumnHeader">
            <setter property="Template">
                <setter.value>
                    <controltemplate targettype="dataPrimitives:DataGridColumnHeader">
                        <grid x:name="Root" xmlns:x="#unknown">
                            <grid.columndefinitions>
                                <columndefinition />
                                <columndefinition width="Auto" />
                            </grid.columndefinitions>
                            <rectangle x:name="VerticalSeparator" fill="{TemplateBinding SeparatorBrush}" verticalalignment="Stretch" width="1" visibility="{TemplateBinding SeparatorVisibility}" grid.column="1" />
                            <checkbox x:name="chkSelectAll" click="chk_Click" ischecked="True">
                                Margin="2,0,0,0" Content="Select" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                        </checkbox></grid>
                    </controltemplate>
                </setter.value>
            </setter>
        </Style>
</usercontrol.resources>

<datagrid>
<data:datagridtemplatecolumn width="80" canuserreorder="False" headerstyle="{StaticResource DataGridColumnHeaderStyle}" xmlns:data="#unknown">
 </data:datagridtemplatecolumn>
</datagrid>




并在代码中





And in Code


Grid _chkSelAllGrid = (Grid)DataGrid_QFDGetSuggestedTools.Columns[0].Header;

//_chkSelAllGrid  always returns null




请帮我这个.....
在此先感谢...




Please help me on this.....
Thanks in advance...

我可以看到您正在为复选框使用click事件:
I can see you are using the click event for the checkbox here:
<checkbox x:name="chkSelectAll" click="chk_Click" ischecked="True">
                                Margin="2,0,0,0" Content="Select" VerticalAlignment="Center" HorizontalAlignment="Left"/>



click事件将提供事件的发送者,在本例中为CheckBox.



The click event will provide the sender of the event which in this case is a CheckBox.

private void chkSelectAll_Click(object sender, RoutedEventArgs e)
{
    // Access the CheckBox
    CheckBox myCheckBox = (CheckBox)sender;
    ClickedCB = myCheckBox; // Store the checkbox in a property
}



更新:

阿伦(Arun)写道:谢谢塔伦(Tarun.K.S)的回复.这对我来说很好,没有问题.但是我还有另一个按钮.在该按钮的单击事件上,我要访问上述复选框.怎么可能.... ???

如何将复选框存储在属性中.

定义属性:



Update :

Arun wrote : Thank you Tarun.K.S for your reply. This is working fine for me no issues. But I have another button. On the click event of that button I want access the above mentioned checkBox. How it is possible....???

How about storing the checkbox in a property.

Define a property :

private CheckBox _checkBox;
public CheckBox ClickedCB
{
    get { return _checkBox; }
    set { _checkBox=value; }
}

private void Button1_Click(Object sender, RoutedEventArgs e)
{
  if(ClickedCB!=null)
  {
      //Access the property ClickedCB containing the checkbox    
  }
}



这应该做到!



This should do it!