Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
500
XamGrid - How to handle Checked/Unchecked events of the checkboxes in a CheckBoxColumn
posted

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_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - 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_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - 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_CellControlAttached
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - 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_CellControlAttached
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_Click
ProhibitionControl - ProhibitionControl_UnChecked
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - levelXamGrid_CellControlAttached
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - ProhibitionControl_Checked
ProhibitionControl - levelXamGrid_ActiveCellChanged

In this case the behavior is not correct, because I would expect only one Trace output for 'ProhibitionControl - ProhibitionControl_UnChecked'.

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

Parents
  • 12875
    posted

    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.

     

Reply Children