none
How to access the checkbox in wpf toolkit datagrid column header checkbox through code

    General discussion

  • Hi,

    I am using the following code snippet to create a checkbox in column header in wpf toolkit datagrid.

    <my:DataGrid.Columns>
                    <my:DataGridTextColumn Header="Header1">
                        <my:DataGridTextColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding}"/>
                            </DataTemplate>
                        </my:DataGridTextColumn.HeaderTemplate>
                    </my:DataGridTextColumn>
                </my:DataGrid.Columns>
            </my:DataGrid>

    But the issue now is i want to access that column header checkbox through code. For that i need to get the checkbox name. I have used both x:Name and Name property in the xaml file. But still i am unable to access that checkbox through code.... can you please help me in this regard??? :(

    Thanks and Regards,
    Subalakshmi Vijayarajan.
    Thursday, October 15, 2009 4:08 AM

All replies

  • Hi,

    Firstly you need give one name property to your CheckBox control in template. <CheckBox x:Name="cb" Content="{Binding}"/>

    Once you get the expected DataGridTextColumn object, you can use HeaderTemplate.FindName method like this:

    DataGridTextColumn dgtc; //....
    dgtc.HeaderTemplate.FindName("cb", dgtc);

    Please check this post for more details.

    Hope this helps.

    Yiling, MVP(Visual C++)
    Thursday, October 15, 2009 4:51 AM
  • Hi,

    I tried this ... but i get an error saying that

    "Error 7 Argument '2': cannot convert from 'Microsoft.Windows.Controls.DataGridTemplateColumn' to 'System.Windows.FrameworkElement' " and Error 6 The best overloaded method match for 'System.Windows.FrameworkTemplate.FindName(string, System.Windows.FrameworkElement)' has some invalid arguments 

    The code that i used is as follows:

    <

     

    dg:DataGrid Margin="238,107,10,0" Background="Transparent" SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding Path=.}" Name="ShutdownTasksDataGrid" Visibility="Visible" Height="210" VerticalAlignment="Top" IsReadOnly="True" PreviewMouseLeftButtonUp="ShutdownTasksDataGrid_PreviewMouseLeftButtonUp" PreviewKeyDown="ShutdownTasksDataGrid_PreviewKeyDown">

     

     

    <dg:DataGrid.BitmapEffect>

     

     

    <OuterGlowBitmapEffect GlowColor="SteelBlue" />

     

     

    </dg:DataGrid.BitmapEffect>

     

     

    <dg:DataGrid.Columns>

     

     

    <dg:DataGridTemplateColumn x:Name="IsCompleted" Header="C" IsReadOnly="False" Visibility="Visible">

     

     

    <dg:DataGridTemplateColumn.HeaderTemplate>

     

     

    <DataTemplate x:Name="Checking">

     

     

    <CheckBox x:Name="SelectCheckBox" Content="{Binding}" IsChecked="False" IsThreeState="False" Click="HeaderCheckBox_Click"/>

     

     

    </DataTemplate>

     

     

    </dg:DataGridTemplateColumn.HeaderTemplate>

     

     

    <dg:DataGridTemplateColumn.CellTemplate>

     

     

    <DataTemplate>

     

     

    <CheckBox HorizontalContentAlignment="Center" IsChecked="{Binding Path=Complete}" IsThreeState="False" Name="Complete" VerticalContentAlignment="Center" Checked="Complete_Checked" Unchecked="Complete_Unchecked"/>

     

     

    </DataTemplate>

     

     

    </dg:DataGridTemplateColumn.CellTemplate>

     

     

    </dg:DataGridTemplateColumn>

    </

     

    dg:DataGrid.Columns>

     

     

    </dg:DataGrid>

    My need is I want to access the checkbox which is inside <dg:DataGridTemplateColumn.HeaderTemplate> <DataTemplate x:Name="Checking">

    Can you please tell me the problem in this scenario?

    Thanks and Regards,
    Subalakshmi Vijayarajan.

    Thursday, October 15, 2009 5:02 AM
  • Hi 

    Yes, you are right, my previous method does have this issue, we can do it like to work around it.

     private DataGridColumnHeadersPresenter GetColumnHeadersPresenter(DataGrid dataGrid)
    {
                DataGridColumnHeadersPresenter chp = null;
                Control sv = dataGrid.Template.FindName("DG_ScrollViewer", dataGrid) as Control;
                if (sv != null)
                {
                    chp = sv.Template.FindName("PART_ColumnHeadersPresenter", sv) as DataGridColumnHeadersPresenter;
                }
                return chp;
    }

    private DataGridColumnHeader GetColumnHeader(int index, DataGrid dataGrid)
    {
                DataGridColumnHeadersPresenter presenter = GetColumnHeadersPresenter(dataGrid);

                if (presenter != null)
                {
                    return (DataGridColumnHeader)presenter.ItemContainerGenerator.ContainerFromIndex(index);
                }
                return null;
    }

    After that.

    DataGridColumnHeader header = GetColumnHeader(0, dataGrid);
    if(header != null)
    {
            CheckBox tb = header.Template.FindName("cb", header) as CheckBox;
    }

    Hope this helps.

    Yiling, MVP(Visual C++ and Client App Dev)
    Thursday, October 15, 2009 5:54 AM
  • Hi,

    I guess this solution is working fine... till header retrieval there is no problem... but, when i cast it to checkbox i am getting only null values.... what that "cb" means in that code???? is it simply 'Name' or 'x:Name' of checkbox... further we are not using Control template but using checkbox inside Data Template... so i guess some how it is unable to recognise the name of the checkbox in the code.... :( :( :(.....
    can you please post your xaml design for this code so that it would be really helpful for me :)

    Thanks and Regards,
    Subalakshmi Vijayarajan.
    Thursday, October 15, 2009 6:17 AM
  • Hi,

    Is there any solution to access the checkbox control in the column header through the code???? :(

    Thanks and Regards,
    Subalakshmi Vijayarajan.
    Thursday, October 15, 2009 9:30 AM
  • Hi,

    This code is working fine if we create a xaml like this.

    <

     

    Grid.Resources>
    <Style x:Key="defaultColumnHeaderStyle" TargetType="{x:Type dg:DataGridColumnHeader}">
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type dg:DataGridColumnHeader}">
    <StackPanel>
    <CheckBox x:Name="HeaderCheckBox" IsChecked="False" Content="{Binding}" Checked="HeaderCheckBox_Checked" Unchecked="HeaderCheckBox_Unchecked"/>
    </StackPanel>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </Grid.Resources>

    <

     

    dg:DataGridTemplateColumn x:Name="IsCompleted" IsReadOnly="False" Visibility="Visible" HeaderStyle="{StaticResource defaultColumnHeaderStyle}">

     

     

    <!--<dg:DataGridTemplateColumn.HeaderTemplate>

    <DataTemplate>

    <CheckBox x:Name="HeaderCheckBox" Content="{Binding}" IsChecked="False" IsThreeState="False" Click="HeaderCheckBox_Click"/>

    </DataTemplate>

    </dg:DataGridTemplateColumn.HeaderTemplate>-->

     

     

    <dg:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <CheckBox HorizontalContentAlignment="Center" IsChecked="{Binding Path=Complete}" IsThreeState="False" Name="Complete" VerticalContentAlignment="Center" Checked="Complete_Checked" Unchecked="Complete_Unchecked"/>
    </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>

    Thank you so much!!!! :) :) :)

    Thanks and Regards,
    Subalakshmi Vijayarajan.

    Thursday, October 15, 2009 12:05 PM
  • Hello Subalakshmi,

      Thank you for sharing the solution.

     
    Please mark the replies as answers if they help and unmark them if they provide no help
    Friday, October 30, 2009 7:35 AM
  • Hi Subalakshmi,

    In my data grid I have Multiple row so each and every row having the checkbox .my question how I can get the each and every checkbox name....Because I am using it like

    <dg:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <CheckBox HorizontalContentAlignment="Center" IsChecked="{Binding Path=Complete}" IsThreeState="False" Name="Complete" VerticalContentAlignment="Center" Checked="Complete_Checked" Unchecked="Complete_Unchecked"/>
    </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>

    please help..

    Thursday, November 11, 2010 1:49 AM
  • Hi,

    do you have the checkbox in grid cells or grid column header? If you are using the checkbox in the grid cells, you can access them as follows:

    DataRowView currentRow = (DataRowView)myDataGrid.SelectedItem;
    currentRow.Row["Complete"] = "True"; //this will check the checkbox in the "Complete" column of hte selected row
    currentRow.Row["Complete"] = "False"; //this will uncheck the checkbox in the "Complete" column of hte selected row

    Hope this helps you

    Thanks & Regards,
    Subalakshmi Vijayarajan.


    Subalakshmi Vijayarajan.
    Thursday, November 11, 2010 9:35 AM
  • HI I am facing a problem with this functionality


    I want to access the button which is inside the WPF datagrid header. We tried some methods mentioned by you. But no effect..

    The XAML part of the datagrid is shown below

    <my:DataGridTextColumn Binding="{Binding Path=drugName}" Width="400"  Header="Medication">
                                    <my:DataGridTextColumn.HeaderTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Text="Medication"></TextBlock>
                                                <Button Name="btnPopUpMedication" Height="22" Width="Auto" Content="Add Medication"></Button>
                                            </StackPanel>
                                        </DataTemplate>
                                    </my:DataGridTextColumn.HeaderTemplate>
                                </my:DataGridTextColumn>

     

     

    Here i want to access the button named 'btnPopUpMedication' which is inside the datagrid header. Please clarify this thing with proper solution..Waiting for that

     

    Friday, December 17, 2010 2:46 PM
  • Hi,

    Please try this.

    <Grid.Resources>
    <Style x:Key="defaultColumnHeaderStyle" TargetType="{x:Type dg:DataGridColumnHeader}">
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type dg:DataGridColumnHeader}">
    <StackPanel>
    <Button x:Name="MyButton" Content="Header"/>
    </StackPanel>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </Grid.Resources>

    <dg:DataGridTemplateColumn x:Name="IsCompleted" IsReadOnly="False" Visibility="Visible" HeaderStyle="{StaticResource defaultColumnHeaderStyle}">
    <dg:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <CheckBox HorizontalContentAlignment="Center" IsChecked="{Binding Path=Complete}" IsThreeState="False" Name="Complete" VerticalContentAlignment="Center" Checked="Complete_Checked" Unchecked="Complete_Unchecked"/>
    </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>

     

    Thanks & Regards,
    Subalakshmi Vijayarajan.


    Subalakshmi Vijayarajan.
    Wednesday, December 22, 2010 4:34 AM