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
很高兴解决。我认为很多人都有这个问题,我知道我当然做到了! –