Hello,
first of all a "Happy New Year" to the support team!
I need to know the best way how to attach event handler to the CheckBox of a CheckBoxColumn for handling Checked/Unchecked events.
Up to now we attached the event handler in the event handler for CellControlAttached:
private void levelXamGrid_CellControlAttached(object sender, CellControlAttachedEventArgs e){ if (((Infragistics.Controls.Grids.CellBase)e.Cell).Column is Infragistics.Controls.Grids.CheckBoxColumn) { System.Diagnostics.Trace.WriteLine("ProhibitionControl - levelXamGrid_CellControlAttached");
(e.Cell.Control.Content as System.Windows.Controls.CheckBox).Checked += ProhibitionControl_Checked; (e.Cell.Control.Content as System.Windows.Controls.CheckBox).Unchecked += ProhibitionControl_Unchecked; }}
At the moment the Checked/Unchecked event handler only do some Trace output:
void ProhibitionControl_Checked(object sender, RoutedEventArgs e){ System.Diagnostics.Trace.WriteLine("ProhibitionControl - ProhibitionControl_Checked");}
void ProhibitionControl_Unchecked(object sender, RoutedEventArgs e){ System.Diagnostics.Trace.WriteLine("ProhibitionControl - ProhibitionControl_UnChecked");}
The grid layout is set up in the following method:
private bool CreateGridLayout(XamGrid gridTable){ if (Levels1 == null || Levels1.Count == 0 || Levels2 == null || Levels2.Count == 0) { return false; }
// Create the Grid-Layout gridTable.Columns.Clear(); gridTable.ItemsSource = null; gridTable.HeaderRowHeight = RowHeight.Dynamic;
TextColumn col = new TextColumn(); col.Key = "Name"; col.HeaderText = "Name"; gridTable.Columns.Add(col); foreach (CCJLevel level2 in Levels2) { CheckBoxColumn chckcol = new CheckBoxColumn(); chckcol.Key = "lev" + level2.ID.ToString(); chckcol.HeaderText = level2.Name; chckcol.EditorDisplayBehavior = EditorDisplayBehaviors.Always; gridTable.Columns.Add(chckcol); }
return true;}
When initializing the grid, you can see in the attached file 'ProhibXamGridWithCheckBoxColumn.PNG', I get the following Trace output:
ProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttached
'ProhibitionControl - levelXamGrid_CellControlAttached' is traced 12 times because the grid contains 12 Checkboxes.'ProhibitionControl - ProhibitionControl_Checked' is traced 11 times because 11 Checkboxes are checked.In this case the behavior is correct.
If I then check the last unchecked Checkbox I get the following Trace output:
ProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_ActiveCellChanged
In this case the behavior is not correct, because I would expect only one Trace output for 'ProhibitionControl - ProhibitionControl_Checked'.
If I then uncheck the last checked Checkbox again I get the following Trace output:
ProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_Click
In this case the behavior is not correct, because I would expect only one Trace output for 'ProhibitionControl - ProhibitionControl_UnChecked'.
If I then uncheck the first checked Checkbox I get the following Trace output:
ProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_ClickProhibitionControl - ProhibitionControl_UnCheckedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - levelXamGrid_CellControlAttachedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - ProhibitionControl_CheckedProhibitionControl - levelXamGrid_ActiveCellChanged
My questions:1. What is the best way to handle the case, when the selection of a CheckBox changes?2. Do we really need two event handler for Checked/Unchecked events?3. How should we attach the event handler for Checked/Unchecked events?4. How can we achieve that the specific handler is called only once when the selection (respectively the check state) of a CheckBox changes?
Please open the attached file 'ProhibXamGridWithCheckBoxColumn.PNG'.
Regards,chrisobs
Hi,
Happy New Year to you as well.
Where you have added your code in the CellControlAttached event you need to clear the event first, instead of repeatedly adding the event.
(e.Cell.Control.Content as System.Windows.Controls.CheckBox).Checked -= ProhibitionControl_Checked; (e.Cell.Control.Content as System.Windows.Controls.CheckBox).Checked += ProhibitionControl_Checked;
(e.Cell.Control.Content as System.Windows.Controls.CheckBox).Unchecked -= ProhibitionControl_Unchecked; (e.Cell.Control.Content as System.Windows.Controls.CheckBox).Unchecked += ProhibitionControl_Unchecked;
Please let me know if you have any questions.
Hello Marianne,
thank you for the support.
Now the event handler are called only once.
According to your answer I suppose there is no better way to handle Check/Uncheck events.
Regards,
chrisobs
everything works now like expected.
Thank you very much!
My suggestion was based on the fact that you are attaching the Check/Uncheck events during the CellControlAttached event, which occurs respectively whenever a cell comes into view.
The idea was to have only one set of Check/Uncheck events attached to a CheckBoxColumn cell.