简体   繁体   English

需要帮助:DataGridCheckBoxColumn双向仅可单向工作

[英]Need help: DataGridCheckBoxColumn two-way works only one-way

I (..still learning wpf..) made a DataGrid with the first column showing that a signal is enabled or not. 我(..仍在学习wpf ..)制作了一个DataGrid,第一列显示是否启用了信号。

In the xaml: 在xaml中:

    <DataGrid.Columns> ...
      <DataGridCheckBoxColumn Width ="30" Header="" IsReadOnly="False" Binding="{Binding IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </DataGrid.Columns>

The ItemSouce of DataGrid is correctly set and bound with the data in list of ObservableCollection<Signal> signalList . 正确设置了DataGrid的ItemSouce并与ObservableCollection<Signal> signalList列表中的数据绑定。 Everything is correctly shown in DataGrid. 一切都正确显示在DataGrid中。 So the binding from signalList to DataGrid here is working fine. 因此,从这里的signalList到DataGrid的绑定工作正常。

On the other side, I want that with every change of signalList, the DataGrid can update itself automatically. 另一方面,我希望对signalList进行每次更改,DataGrid都可以自动更新自身。 However, if I do 但是,如果我这样做

signalList[0].IsEnabled = true;

The DataGrid doesn't get updated. DataGrid不会更新。 I searched a lot but still can't find the answer. 我进行了很多搜索,但仍然找不到答案。

Did I miss something? 我错过了什么? Thx. 谢谢。

Edit1: 编辑1:

DataGrid do get updated, only if I click another row, and then draw the scroller out of sight. 仅当我单击另一行,然后将滚动条拖到视线之外时,DataGrid才会更新。 Then if I draw the scroller back, the row is correctly shown. 然后,如果我向后拖动滚动条,则该行将正确显示。 I think I definitively missed something, can someone give me a hint? 我想我肯定错过了什么,有人可以给我提示吗?

I solved the problem with help from Aybe and gavin. 我在Aybe和gavin的帮助下解决了这个问题。 For record, I add my code here: 为了记录,我在这里添加代码:

class Signal : INotifyPropertyChanged 信号类:INotifyPropertyChanged

{
...
    private bool isEnabled;
    public bool IsEnabled
    {
      get { return isEnabled; }
      set { isEnabled = value; OnPropertyChanged(new PropertyChangedEventArgs("IsEnabled"));}
    }

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
  if (PropertyChanged != null)
    PropertyChanged(this, e);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM