[英]WPF switching column visibility in DataGrid triggers unwanted event
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"; 我有一个标签lblUpdateMode
,其文本为“单行”或“每行”;
A datagrid with 10 columns, where initially 5 columns are visible and 5 are hidden; 一个具有10列的数据网格,其中最初5列是可见的,而5列是隐藏的;
Pressing a button btnChangeView
flips visibility of each column; 按下按钮btnChangeView
翻转每列的可见性;
In one of the data grid columns I have a CheckBoxColumn, with Checked/Unchecked events. 在其中一个数据网格列中,我有一个CheckBoxColumn,其中包含Checked / Unchecked事件。 If label text = "every row"
pressing on a single checkbox updates every row. 如果标签text = "every row"
单击单个复选框将更新每一行。
However, if label = "every row"
and I press on btnChangeView
, it also triggers Checked event and updates checkboxes in every row. 但是,如果label = "every row"
并且我按btnChangeView
,它还会触发Checked事件并更新每一行中的复选框。
Why is this happening and how can I avoid it? 为什么会发生这种情况,我该如何避免呢?
Here's the code to the Checked event - nothing fancy or strange: 这是Checked事件的代码-没什么奇怪的:
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: 这是此列的XAML:
<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 谢谢
In your XAML
, you bind the IsChecked
property of your CheckBox
to a property called Import
. 在XAML
,将CheckBox
的IsChecked
属性绑定到名为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, 但是,绑定Mode
设置为TwoWay
,这意味着,
IsChecked
property of the CheckBox
in your XAML
, that change will be reflected in your Import
property in your code, 如果XAML
CheckBox
的IsChecked
属性的值发生了变化,则该变化将反映在代码中的Import
属性中, and, 和,
Import
property in your code, that change will be reflected in the actual CheckBox
in your XAML
. 如果代码中Import
属性的值发生变化,则该变化将反映在XAML
的实际CheckBox
中。 In other words, if the user checks the check box, Import
value will be set to true
. 换句话说,如果用户选中该复选框,则Import
值将设置为true
。 If user unchecks the check box, Import
will be false
. 如果用户取消选中该复选框,则Import
将为false
。 Conversely, if you change the value of Import
to false
at any point in your code, your GUI check box will be unchecked. 相反,如果您在代码中的任何时候将Import
的值更改为false
,则GUI复选框都将处于未选中状态。 If Import
is set to true
in code, GUI check box will be checked. 如果在代码中将Import
设置为true
,则将选中GUI复选框。
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
. 我假设在btnChangeView
的Click
偶数中(您未在帖子中显示),您更改了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
. 因此, Mode=TwoWay
假设您要设置Mode=OneWayToSource
,而不是Mode=TwoWay
,而是假设您希望在用户选中/取消选中CheckBox
时执行某些操作。
Read this for a detailed explanation of binding modes in WPF. 阅读本文档以获取WPF中绑定模式的详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.