0

NOTE: I knew where I'm getting null but don't know how to set reference to it for that particular scenario.Checking & unchecking very thing works perfect. Only Key press event handling for checkbox at header column I'm facing trouble.

I had CheckBox on a column header and default focus is on that check box. I also have DataGrid_KeyDownEvents() where it handles Enter key event. when ever I press Enter Key it CurrentCell.Column.Header Column gets null when the focus is on Column Header checkbox. my question is how do I can set object reference instance of an object correctly.

<DataGrid.Columns>
    <DataGridTemplateColumn Header="" Width="50" >
         <DataGridTemplateColumn.HeaderTemplate> <!--<column header>-->
             <DataTemplate>

                 <CheckBox DataContext="{Binding ElementName=xyz, Path=DataContext}"  
                  IsChecked="{Binding IsAllChecked, Mode=TwoWay}"  Style="{DynamicResource CheckBoxStyle}" HorizontalAlignment="Center"  VerticalAlignment="Center"></CheckBox>

             </DataTemplate>
         </DataGridTemplateColumn.HeaderTemplate>

         <DataGridTemplateColumn.CellTemplate> <!--<rows>-->
             <DataTemplate>

                   <CheckBox Style="{DynamicResource CheckBoxStyle}" HorizontalAlignment="Center"  VerticalAlignment="Center" IsChecked="{Binding IsMarked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding CanBeMarked, Converter={StaticResource BoolToVisConverter}}"></CheckBox>

         </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>

DataGrid_KeyDownEvents()

if ((e.Key == Key.Enter || e.Key == Key.Return))
           {
                if (this.CurrentCell.Column.Header.ToString() == XColumnName)
                {
                    //do something
                }
           }

What I'm excepting from above code is <DataGridTemplateColumn.HeaderTemplate> check box on column header reference is to handle but I'm getting Null reference in column object this.CurrentCell.Column.Header. and the check boxes in gridRows are getting reference as good as enough.

k11k2
  • 2,036
  • 2
  • 22
  • 36
  • @Reniuz I explained where I'm getting null my problem is how to set reference for it. Could you please go through it. – k11k2 Nov 30 '17 at 10:53
  • Although this question has been marked as a duplicate so I can't actually answer, I would say that you need to check you have a CurrentCell before you try and get the Column, and then check you have a column before trying to find the header etc. I would also look at the "e" and sender objects to see if you can get the instance that is generating the key. You don't need to handle keydown for the column header as you are using a binding, that binding should be handling the flagging of all items to true or false. – netniV Dec 01 '17 at 14:37
  • @netniV Thanks for your response. CurrentCell is getting reference but column is null. My concern is not like to check it as null or Empty.I want to handle it in some other way.. Is there any chance of it ? like setting reference for column header or making header disable for keydown operations. Is there any such type of chances to handle this? – k11k2 Dec 04 '17 at 04:54

0 Answers0