0

I have a weird issue where changing data grid column visibility with a press of a button, it triggers a "Checked" event tied to a checkbox in that data grid column.

So here's my setup:

  • I have a label lblUpdateMode with a text either "single row" or "every row";

  • A datagrid with 10 columns, where initially 5 columns are visible and 5 are hidden;

  • Pressing a button btnChangeView flips visibility of each column;

  • In one of the data grid columns I have a CheckBoxColumn, with Checked/Unchecked events. If label text = "every row" pressing on a single checkbox updates every row.

However, if label = "every row" and I press on btnChangeView, it also triggers Checked event and updates checkboxes in every row.

Why is this happening and how can I avoid it?

Here's the code to the Checked event - nothing fancy or strange:

private void UpdateDataGridCheckBox(string colname, bool v)
{
    if (lblUpdateMode.Content.ToString() == "Every Row")
    {
        foreach (DataRow dr in DataAccess.Instance.sourceFiles.Rows)
        {
            dr[colname] = v;
        }
    }
}

And here's the XAML for this column:

<DataGrid x:Name="dataGridFiles" Grid.Row="2" Margin="10" Visibility="Collapsed"
                      ItemsSource="{Binding Path=DataAccess.Instance.sourceFiles, Mode=TwoWay}">
            <DataGrid.Columns>
                <!--Import-->
                <DataGridTemplateColumn Header="Import" Width="60" CanUserReorder="True" Visibility="Collapsed">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="checkBoxImport" HorizontalAlignment="Center" VerticalAlignment="Center"
                                  IsChecked="{Binding Path=Import, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Checked="checkBoxImport_Checked"
                                  Unchecked="checkBoxImport_Unchecked"></CheckBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
</DataGrid.Columns>

Thanks

  • 1
    Please, post your xaml. and the btnChangeView code behind – Magnetron Jul 26 '18 at 17:01
  • Likely a problem with the check box trigger association in your `XAML`. Yeah please post the `XAML` and the button code behind. – Sach Jul 26 '18 at 17:38
  • I've added XAML for the one of the checkbox. Code behind for the button is very simple: grid.Columns[0].Visibility = Visibility.Visible; grid.Columns[1].Visibility = Visibility.Collapsed; grid.Columns[2].Visibility = Visibility.Collapsed; grid.Columns[3].Visibility = Visibility.Visible; – Darius Garuolis Jul 27 '18 at 08:23
  • @Sach, is this something you can assist me with? Thanks. – Darius Garuolis Jul 30 '18 at 16:09
  • What is the `Import` property in your code behind? And where do you change the value of that in the code? – Sach Jul 30 '18 at 16:25

1 Answers1

0

In your XAML, you bind the IsChecked property of your CheckBox to a property called Import.

<CheckBox x:Name="checkBoxImport" HorizontalAlignment="Center" VerticalAlignment="Center"
          IsChecked="{Binding Path=Import, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          Checked="checkBoxImport_Checked"
          Unchecked="checkBoxImport_Unchecked"></CheckBox>

However, the binding Mode is set to TwoWay, which means,

  • If there's a change of value in the IsChecked property of the CheckBox in your XAML, that change will be reflected in your Import property in your code,

and,

  • If there's a change of value in the Import property in your code, that change will be reflected in the actual CheckBox in your XAML.

In other words, if the user checks the check box, Import value will be set to true. If user unchecks the check box, Import will be false. Conversely, if you change the value of Import to false at any point in your code, your GUI check box will be unchecked. If Import is set to true in code, GUI check box will be checked.

I'm assuming that in the Click even of the btnChangeView (which you haven't shown in your post), you change the value of Import.

So, instead of Mode=TwoWay, assuming that you want to do something when the user checks/unchecks the CheckBox, I think you want to set Mode=OneWayToSource.

Read this for a detailed explanation of binding modes in WPF.

Sach
  • 10,091
  • 8
  • 47
  • 84