Search code examples
wpf2-way-object-databinding

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.

In the 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. Everything is correctly shown in DataGrid. So the binding from signalList to DataGrid here is working fine.

On the other side, I want that with every change of signalList, the DataGrid can update itself automatically. However, if I do

signalList[0].IsEnabled = true;

The DataGrid doesn't get updated. I searched a lot but still can't find the answer.

Did I miss something? Thx.

Edit1:

DataGrid do get updated, only if I click another row, and then draw the scroller out of sight. Then if I draw the scroller back, the row is correctly shown. I think I definitively missed something, can someone give me a hint?


Solution

  • I solved the problem with help from Aybe and gavin. For record, I add my code here:

    class Signal : 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);
    }
    }