【问题标题】:Toggle Checkbox value after validation [duplicate]验证后切换复选框值[重复]
【发布时间】:2020-03-03 11:46:41
【问题描述】:

如果 DataGridCheckboxColumn 在另一列中的数量超过一定数量,我正在尝试切换复选框。

到目前为止,它遍历 DataGrid 并成功验证了我需要它的另一列,但我无法切换任何符合条件的 Chekbox。

<DataGridTextColumn Header="Total" IsReadOnly="True" Binding="{Binding cost}" Width="130"></DataGridTextColumn>

<DataGridCheckBoxColumn Header="Check" IsReadOnly="False" Width="50">
    <DataGridCheckBoxColumn.ElementStyle>
        <Style />
    </DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
for(int i=0; i<dataGrid.Items.Count - 1; i++)
{
    if(Convert.ToDecimal(dataGrid.Columns[3].GetCellContent(i)) > 200)
        {            //Columns[3] is the "Total" Column.
             ((dataGrid.Columns[6]).GetCellContent(i) as CheckBox).IsChecked = true;
             //Columns[6] is the DataGridCheckBoxColumn
        }
}

我使用以下方法获得了 Total 列的正确值:

dataGrid.Columns[3].GetCellContent(i)

但是

((dataGrid.Columns[6]).GetCellContent(i) as CheckBox);

这是 DataGridCheckBoxColumn,返回一个 nullException,即使它是正确的列。如果我 WriteLine dataGrid.Columns[6],我会得到“System.Windows.Controls.DataGridCheckBoxColumn”,所以我确定我在正确的位置,但我无法将其设置为 true。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    尝试将Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" 属性添加到您的DataGridCheckBoxColumn,如下所示:

    <DataGridCheckBoxColumn Header="Check" IsReadOnly="False" Width="50" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" >
        ...
    

    您还可以检查以下堆栈溢出问题: How to perform Single click checkbox selection in WPF DataGrid with IEditableObject object

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2013-04-19
      • 2016-07-17
      • 2017-08-04
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      相关资源
      最近更新 更多