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的,但我不知道如何...
太棒了! 隨着你的解釋我已經理解,現在完美的作品! 非常感謝! – user2263764
很高興解決。我認爲很多人都有這個問題,我知道我當然做到了! –