Thursday 15 May 2014

c# - ItemsSource to ObservableCollection doesn't refresh if change items inside it -


In my application I have a datagrid containing its itemsource ObsavableCollection & lt; Carrello & gt; .

  & lt; Grid X: Name = "DatiCarrello" & gt; & Lt; DataGrid name = "carlo" roshelia = "{static resource row level with altutation}" alternation quote = "2" autoconct columns = "falls" margin = "40 9, 250, 30" & gt; & Lt; DataGrid.Columns & gt; & Lt; DataGridTextColumn Header = "id" binding = "{binding path = id_product}}"> & gt; & Lt; DataGridTextColumn header = "description" binding = "{binding path = description}" width = "100" /> [...] & lt; / DataGrid.Columns & gt; & Lt; / Data grid & gt; & Lt; / Grid & gt;   

My class carrello:

  using the system; Using System.Collections.ObjectModel; Using System.ComponentModel; Namespaces WpfApplication1.Classi {class Carrello {public string ID_prodotto {get; Set; } Public string descrizione {get; Set; } Public Double Proseau {Received; Set; } Public int Quantita {get; Set; } Public Entry Sconto {get; Set; } Public int Quantita_massima {get; Set; } Private Static Observation Collection & lt; Carrello & gt; ProdottiInCarrello = New Observation Collection & Lt; Carlo & gt; (); Public static zero clearElencoProdottiInCarrello () {ProdottiInCarrello.Clear (); } Public static observational selection & lt; Carlo & gt; GetElencoProdottiInCarrello () {Return ProdottiInCarrello; } Public static zero InserciProdottoInCarrello (Carrello item) {foreach (Carrello element in ProdottiInCarrello) if (element.ID_prodotto == items.ID_prodotto) {element.Quantita + items.Quantita; Return; } ProdottiInCarrello.Add (item); }}}   

And how do I use it:

  Public Partial Category FinestraCassa: UserControl {Personal Carrello prodotto_carrello = new Carrello (); Public Fincracosa () {Initial Group (); } Private Zero Windowloaded (object sender, RoutedEventArgs e) {DatiCarrello.DataContext = prodotto_carrello; Carrello.ClearElencoProdottiInCarrello (); Carrello.ItemsSource = Carrello.GetElencoProdottiInCarrello (); } Private Zero qta_articolo_KeyDown (Object Sender, KeyEventArgs e) {if (e.Key == Key.Enter) {int sconto = 0; Int.TryParse (sconto_articolo.Text.Replace ("%", ""), outside Scottton); Prodotto_carrello.Sconto = Scota; Carrello.InserciProdottoInCarrello (prodotto_carrello); / * New Elements per Pulisco * / prodotto_carrello = New Carrello (); DatiCarrello.DataContext = prodotto_carrello; TextBoxSearch.Focus (); }}}   

For each new product that I have inserted the datagrid is notified properly, and it displays a new line in. The problem is that when I enter the same product, Only the amount should be updated (if it is already on the list). Effectively updated, but not immediately refreshed, but I have to click within the cell "quantity" to see the changes.

Should I implement INotifyPropertyChanged , but I do not understand that ...

In order to make changes to your property in the 'Carlo' category in the background, you should implement it like this ...

  Public category Carrello : INotifyPropertyChanged {Private string _id_prodotto; Public string ID_prodotto {get {return_id_prodotto; } Set {if (value! = _id_prodotto) {_id_prodotto = value; OnPropertyChanged ("ID_prodotto"); }}} // // Do the same thing for other public properties, which you are bound to // public events; PropertyChangedEventHandler PropertyChanged; Protected Virtual Zero OnPerfect Variation (String Name) {PropertyChangedEventHandler Handler = System.Threading.Interlocked.CompareExchange (Reference Align, Blank, Blank); If (handler! = Null) {handler (new, new property change event event (name)); }}}   

This will increase the appropriate notification, and this will result in the behavior you are seeing.

No comments:

Post a Comment