Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :

XML
<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


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

//_chkSelAllGrid  always returns null



Please help me on this.....
Thanks in advance...
Posted
Updated 6-Apr-11 22:40pm
v2

1 solution

I can see you are using the click event for the checkbox here:
XML
<checkbox x:name="chkSelectAll" click="chk_Click" ischecked="True">
                                Margin="2,0,0,0" Content="Select" VerticalAlignment="Center" HorizontalAlignment="Left"/>


The click event will provide the sender of the event which in this case is a CheckBox.
C#
private void chkSelectAll_Click(object sender, RoutedEventArgs e)
{
    // Access the CheckBox
    CheckBox myCheckBox = (CheckBox)sender;
    ClickedCB = myCheckBox; // Store the checkbox in a property
}


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 :

C#
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!
 
Share this answer
 
v3
Comments
Arun India 7-Apr-11 10:10am    
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....???
Tarun.K.S 7-Apr-11 13:13pm    
Check my updated answer.
Arun India 7-Apr-11 23:03pm    
Tarun, Actually I need the check Box in the Datagrid Header. So I wrote a style(As CheckBox) for the DataGridHeader[See My XAML code]. Now I want to access that checkBox in code. So how I can use the Property method instead of Style?. Please help...
Tarun.K.S 8-Apr-11 1:52am    
I have used that property in this function :
private void chkSelectAll_Click(object sender, RoutedEventArgs e),

check this function in my answer, I wrote ClickedCB = myCheckBox;
Arun India 8-Apr-11 2:12am    
This will Work.. No doubt. Because we are attaching an event to the CheckBox in its Style(Xaml Code). We can access that check box only when you click on it Right..? But this is not My case. I have another button Lets Say _MyButton. On the Click event of this button I want to access the above mentioned CheckBox. Is it possible..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900