ItemsSource to ObservableCollection <t>如果更改其中的项目</t>则不刷新

时间:2013-06-22 15:55:49

标签: c# wpf datagrid inotifypropertychanged

在我的应用程序中,我有一个datagrid,其ItemsSource ObservableCollection&lt; Carrello&gt;

<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 ,但我无法理解......

1 个答案:

答案 0 :(得分: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));
            }
        }
    }

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