2013-06-22 37 views
2

在我的应用程序中,我有一个datagrid具有作为其ItemsSource ObservableCollection < Carrello>ItemsSource to ObservableCollection <T>不刷新,如果它里面的更改项目

<Grid x:Name="DatiCarrello"> 
    <DataGrid Name="carrello" RowStyle="{StaticResource RowStyleWithAlternation}" AlternationCount="2" AutoGenerateColumns="False" Margin="40,95,250,30"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="ID" Binding="{Binding Path=ID_prodotto}" /> 
      <DataGridTextColumn Header="Descrizione" Binding="{Binding Path=Descrizione}" Width="100"/> 
      [...] 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

我班Carrello:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace WpfApplication1.Classi 
{ 
    class Carrello 
    { 
     public string ID_prodotto { get; set; } 
     public string Descrizione { get; set; } 
     public double Prezzo { get; set; } 
     public int Quantita { get; set; } 
     public int Sconto { get; set; } 
     public int Quantita_massima { get; set; } 

     private static ObservableCollection<Carrello> ProdottiInCarrello = new ObservableCollection<Carrello>(); 


     public static void ClearElencoProdottiInCarrello() 
     { 
      ProdottiInCarrello.Clear(); 
     } 

     public static ObservableCollection<Carrello> GetElencoProdottiInCarrello() 
     { 
      return ProdottiInCarrello; 
     } 

     public static void InserciProdottoInCarrello(Carrello items) 
     { 
      foreach (Carrello element in ProdottiInCarrello) 
       if (element.ID_prodotto == items.ID_prodotto) 
       { 
        element.Quantita += items.Quantita; 
        return; 
       } 

      ProdottiInCarrello.Add(items); 
     } 
    }  
} 

这是我如何使用它:

public partial class FinestraCassa : UserControl 
    { 
     private Carrello prodotto_carrello = new Carrello(); 

     public FinestraCassa() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      DatiCarrello.DataContext = prodotto_carrello; 
      Carrello.ClearElencoProdottiInCarrello(); 
      carrello.ItemsSource = Carrello.GetElencoProdottiInCarrello(); 
     } 

private void qta_articolo_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.Enter) 
      { 
       int sconto = 0; 
       int.TryParse(sconto_articolo.Text.Replace("%", ""), out sconto); 

       prodotto_carrello.Sconto = sconto; 

       Carrello.InserciProdottoInCarrello(prodotto_carrello); 


       /* Pulisco per nuovo elemento */ 
       prodotto_carrello = new Carrello(); 
       DatiCarrello.DataContext = prodotto_carrello; 

       TextBoxSearch.Focus(); 
      } 
     } 
} 

对于我插入DataGrid中被适当地通知每一个新产品,并显示新在它的线。 问题是,当我插入相同的产品时,它应该只更新金额(如果它已经在列表中)。有效更新金额,但刷新不立即执行,但我必须在单元格“数量”内单击以查看更改。

我应该实现INotifyPropertyChanged的,但我不知道如何...

回答

2

为了拿起属性更改,您对您的“Carello”类的背景下,你应该实现它像这样...

public class 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 all the other public properties you are binding to 
// 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = System.Threading.Interlocked.CompareExchange(ref 
          PropertyChanged, null, null); 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
    } 

这将引发适当的通知,这将导致您正在寻找的行为。

+0

太棒了! 随着你的解释我已经理解,现在完美的作品! 非常感谢! – user2263764

+0

很高兴解决。我认为很多人都有这个问题,我知道我当然做到了! –

相关问题