private void Button_Click(object sender, RoutedEventArgs e) { //this.sbShowListViewItem.Begin(); // ATTN: Please note it's a "TrulyObservableCollection" that's instantiated. Otherwise, "Trades[0].Qty = 999" will NOT trigger event handler "Trades_CollectionChanged" in main. // REF: http://stackoverflow.com/questions/8490533/notify-observablecollection-when-item-changes TrulyObservableCollection<Trade> Trades = new TrulyObservableCollection<Trade>(); Trades.Add(new Trade { Symbol = "APPL", Qty = 123 }); Trades.Add(new Trade { Symbol = "IBM", Qty = 456 }); Trades.Add(new Trade { Symbol = "CSCO", Qty = 789 }); Trades.CollectionChanged += Trades_CollectionChanged; //Trades.ItemPropertyChanged += PropertyChangedHandler; Trades.RemoveAt(2); Trades[0].Qty = 999; }
/// <summary> /// When an griditem is dropped /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainView_DataGrid_Drop(object sender, System.Windows.DragEventArgs e) { _fileList = _viewModel.List; if (rowIndex < 0) { return; } int index = this.GetCurrentRowIndex(e.GetPosition); if (index < 0) { return; } if (index == rowIndex) { return; } if (index == MainView_DataGrid.Items.Count - 1) { System.Windows.MessageBox.Show("This row-index cannot be dropped"); return; } MyFile tempFile = MainView_DataGrid.SelectedItem as MyFile; _fileList.RemoveAt(rowIndex); _fileList.Insert(index, tempFile); int i = 1; foreach (MyFile file in _fileList) { file.Position = i; i++; } _viewModel.List = _fileList; MainView_DataGrid.ItemsSource = null; MainView_DataGrid.ItemsSource = _viewModel.List; }
public void ShouldRaiseEventWhenItemIsAddedRemovedOrEdited() { var eventRaised = false; var collection = new TrulyObservableCollection <MockNotifiable>(); collection.CollectionChanged += (s, e) => eventRaised = true; eventRaised = false; collection.Add(new MockNotifiable()); Assert.That(eventRaised, Is.True); eventRaised = false; // The collection is supposed to raise the event if the property of one inner element is modified // This is the only added feature to the genuine ObservableCollection<> collection.First().Property = 100; Assert.That(eventRaised, Is.True); eventRaised = false; collection.RemoveAt(0); Assert.That(eventRaised, Is.True); }