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