C# WPF-从字典中删除项时不刷新DataGrid中的数据

C# WPF-从字典中删除项时不刷新DataGrid中的数据,c#,wpf,dictionary,datagrid,C#,Wpf,Dictionary,Datagrid,我在绑定字典、删除和向字典中添加元素方面有问题 第1条: 我有一个名为“Produkt”的基类 public class Produkt : ICloneable, INotifyPropertyChanged { private string producent; public string Producent { get { return producent; } set { producent = value; On

我在绑定字典、删除和向字典中添加元素方面有问题

第1条:

我有一个名为“Produkt”的基类

public class Produkt : ICloneable, INotifyPropertyChanged
{

    private string producent;
    public string Producent
    {
        get { return producent; }
        set { producent = value;
        OnPropertyChanged("Producent");
        }
    }


    private string nameProduct;
    public string NameProduct
    {
        get { return nameProduct; }
        set { nameProduct = value;
        OnPropertyChanged("NameProduct");
        }
    }

    private double cena;
    public double Cena
    {
        get { return cena; }
        set { cena = value;
        OnPropertyChanged("Cena");
        }
    }



    private int ilosc;
    public int Ilosc
    {
        get { return ilosc; }
        set { ilosc = value;
        OnPropertyChanged("Ilosc");
        }
    }

    private String kodKreskowy; // prawie jak xD
    public String KodKreskowy
    {
        get { return kodKreskowy; }
        set { kodKreskowy = value;
        OnPropertyChanged("KodKreskowy");
        }
    }






    protected Produkt(string Producent, string NazwaProduktu, double Cena, int Ilosc)
    {
        this.Producent = Producent;
        this.nameProduct = NazwaProduktu;
        this.ilosc = Ilosc;
        this.cena = Cena;

        this.KodKreskowy = this.ToString();

    }




    #region INTERFACE IMPLEMENTATION ICloneable
    public virtual object Clone()
    {
        return this.MemberwiseClone();
    }
    #endregion
    public override string ToString()
    {
        return GetKodKreskowy();
    }

    public string GetKodKreskowy()
    {
        return Osom.Enums.GetDescriptionFromEnumValue(Enum.Parse(Osom.MyType.GetTypeByName("AllProducts")[0], this.GetType().Name) as Enum)
            + " # " + Producent
            + " # " + NameProduct
            + " # " + Cena;
    }


    #region INTERFACE IMPLEMENTATION INotifyPropertyChanged
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}
以及这些元素的上下文:

public class ProduktCointeiners
{
    /*
    protected List<Produkt> lista = new List<Produkt>();

    public List<Produkt> Lista
    {
        get { return lista; }
    }*/

    public Dictionary<string, Produkt> dictionary = new Dictionary<string, Produkt>();
    ObservableCollection<Produkt> obserwator;

    public List<Produkt> ToList
    {
        get {
            return dictionary.Values.ToList();
        }
        set
        {
            dictionary.Clear();
            foreach (Produkt prod in value)
            {
                dictionary.Add(prod.KodKreskowy, prod);
            }
        }
    }


    // Get Inne Prod
    #region Destroy Item

    /// <summary>
    /// Usuwa permamentnie element ze słownika
    /// </summary>
    public void DestroyProduct(Produkt product)
    {
        DestroyProduct(product.KodKreskowy);
    }

    /// <summary>
    /// Usuwa permamentnie element ze słownika
    /// </summary>
    public void DestroyProduct(string KodKreskowy)
    {
        for (int i = 0; i < dictionary.Count; i++)
        {

            dictionary.Remove(KodKreskowy);

        }
    }

    #endregion


    #region Remove

    public void RemoveProduct(Produkt product)
    {
        RemoveProduct(product.KodKreskowy, product.Ilosc);
    }


    public void RemoveProduct(string KodKreskowy, int Ilosc)
    {
        foreach (Produkt prod in dictionary.Values)
        {
            if (prod.KodKreskowy == KodKreskowy)
            {
                prod.Ilosc -= Ilosc;
                return;
            }
        }
    }

    #endregion



    #region Add
    public void AddProduct(Produkt product)
    {

        try 
        {

            dictionary[product.KodKreskowy].Ilosc += product.Ilosc;
            if (dictionary[product.KodKreskowy].Cena < product.Cena)
                dictionary[product.KodKreskowy].Cena = product.Cena;


        }
        catch(KeyNotFoundException)
        {
            dictionary.Add(product.KodKreskowy, product);
            return;
        }
    }

    #endregion



    #region Cena


    public void ChangeCost(string KodKreskowy, double Cena)
    {
        dictionary[KodKreskowy].Cena = Cena;
    }


    public void ChangeCost(Produkt product)
    {
        ChangeCost(product.KodKreskowy, product.Cena);
    }




    #endregion



    #region Ilosc

    public void Ilosc_zwieksz(Produkt product)
    {
        Ilosc_zwieksz(product.KodKreskowy, product.Ilosc);
    }


    public void Ilosc_zwieksz(string KodKreskowy, int Ilosc)
    {
        dictionary[KodKreskowy].Ilosc += Ilosc;
    }


    public void Ilosc_zmniejsz(Produkt product)
    {
        Ilosc_zmniejsz(product.KodKreskowy, product.Ilosc);
    }


    public void Ilosc_zmniejsz(string KodKreskowy, int Ilosc)
    {

        dictionary[KodKreskowy].Ilosc -= Ilosc;
        if (dictionary[KodKreskowy].Ilosc < 0)
        {
            MessageBox.Show("yyy... w Magazynie masz UJEMNE ilości przedmiotów! :D\n POZDRO!\nP.S. wartosc została zmieniona na 0");
            dictionary[KodKreskowy].Ilosc = 0;
        }

    }

    #endregion

    //#region INTERFACE IMPLEMENTATION INotifyPropertyChanged
    //protected void OnPropertyChanged(string propertyName)
    //{
    //    if (PropertyChanged != null)
    //    {
    //        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    //    }
    //}

    //public event PropertyChangedEventHandler PropertyChanged;
    //#endregion



}
公共类产品接口
{
/*
受保护列表lista=新列表();
公开名单
{
获取{return lista;}
}*/
公共字典=新字典();
可观测收集观测者;
公共收费表
{
得到{
返回dictionary.Values.ToList();
}
设置
{
dictionary.Clear();
foreach(产品价值)
{
添加(prod.KodKreskowy,prod);
}
}
}
//得到内线的刺激
#区域销毁项目
/// 
///Usuwa permamentnie element ze słownika
/// 
公共产品(Produkt产品)
{
销毁产品(product.KodKreskowy);
}
/// 
///Usuwa permamentnie element ze słownika
/// 
公共产品(字符串KodKreskowy)
{
for(int i=0;i
和GUI(WPF代码):


马加津
和代码隐藏:

public partial class MainWindow 
{

    public static Magazyn.AllMagazyny magazyny;
    public static Klient.Klient klient;

    public Historia.Historia historiaZakupowKlientow;

    public List<Produkt> testing = new List<Produkt>();

    public void InitializeData()
    {
        magazyny = new Magazyn.AllMagazyny(new Magazyn.Magazyn());
        klient = new Klient.Klient();

        historiaZakupowKlientow = new Historia.Historia();


        // Zmiana elementow na wszystkie z enuma
        cb_M_Type.Items.Clear();
        foreach (AllProducts typeProd in Enum.GetValues(typeof(AllProducts)))
        {
            cb_M_Type.Items.Add(Enums.GetDescriptionFromEnumValue(typeProd));
        }


        //CollectionViewSource itemCollectionViewSource;
        //itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
        //itemCollectionViewSource.Source = klient.ToList;




        TESTING_VALUES();
        for (int i = 0; i < 100; i++)
        {
            testing.Add(new PapierosyBezFiltra("asd", "asd", 12.2, 3));
        }

        this.dg_Magazyn.ItemsSource = magazyny.GetAktualnyMagazyn().dictionary.Values;
        this.dg_Klient.ItemsSource = klient.dictionary.Values;

    }




    public void TESTING_VALUES()
    {
        magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent1", "prod1", 12.55, 11));
        magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Smakowy("producent2", "prod2", 22.55, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Tytoniowy("producent3", "prod3", 33, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));

        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 55, 11));

        //for (int i = 0; i < 10; i++)
        //{
        //    magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Smakowy(i.ToString() + ". prod", "nazwa", 1.2 * i * i * i, i));
        //}

        Produkt tmp;
        foreach(Produkt prod in magazyny.GetAktualnyMagazyn().ToList)
        {
            tmp = (Produkt)prod.Clone();
            tmp.Ilosc = 1;
            klient.AddProduct(tmp);
        }


    }

}
公共部分类主窗口
{
公共静态Magazyn.AllMagazyny magazyny;
公共静态Klient.Klient Klient;
公共历史;
公共列表测试=新列表();
公共无效初始化数据()
{
magazyny=new Magazyn.AllMagazyny(new Magazyn.Magazyn());
klient=新klient.klient();
historiaZakupowKlientow=新历史a.Historia();
//Zmiana elementow na wszystkie z enuma
cb_M_Type.Items.Clear();
foreach(Enum.GetValues中的AllProducts类型Prod(typeof(AllProducts)))
{
cb_M_Type.Items.Add(Enums.GetDescriptionFromEnumValue(typeProd));
}
//CollectionViewSource项目CollectionViewSource;
//itemCollectionViewSource=(CollectionViewSource)(FindResource(“itemCollectionViewSource”);
//itemCollectionViewSource.Source=klient.ToList;
测试_值();
对于(int i=0;i<100;i++)
{
测试。添加(新的“asd”、“asd”12.2、3);
}
this.dg_Magazyn.ItemsSource=magazyny.GetAktualnyMagazyn().dictionary.Values;
this.dg_Klient.ItemsSource=Klient.dictionary.Values;
}
公共无效测试_值()
{
magazyny.GetAktualnyMagazyn().AddProduct(新的papierosybezfiltera(“producent1”、“prod1”、12.55、11));
magazyny.GetAktualnyMagazyn().AddProduct(新的papierosyfilter_Smakowy(“产品2”,“产品2”,22.55,11));
//magazyny.GetAktualnyMagazyn().AddProduct(新的papierosyfilter_Tytoniowy(“产品3”,“产品3”,33,11
public partial class MainWindow 
{

    public static Magazyn.AllMagazyny magazyny;
    public static Klient.Klient klient;

    public Historia.Historia historiaZakupowKlientow;

    public List<Produkt> testing = new List<Produkt>();

    public void InitializeData()
    {
        magazyny = new Magazyn.AllMagazyny(new Magazyn.Magazyn());
        klient = new Klient.Klient();

        historiaZakupowKlientow = new Historia.Historia();


        // Zmiana elementow na wszystkie z enuma
        cb_M_Type.Items.Clear();
        foreach (AllProducts typeProd in Enum.GetValues(typeof(AllProducts)))
        {
            cb_M_Type.Items.Add(Enums.GetDescriptionFromEnumValue(typeProd));
        }


        //CollectionViewSource itemCollectionViewSource;
        //itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
        //itemCollectionViewSource.Source = klient.ToList;




        TESTING_VALUES();
        for (int i = 0; i < 100; i++)
        {
            testing.Add(new PapierosyBezFiltra("asd", "asd", 12.2, 3));
        }

        this.dg_Magazyn.ItemsSource = magazyny.GetAktualnyMagazyn().dictionary.Values;
        this.dg_Klient.ItemsSource = klient.dictionary.Values;

    }




    public void TESTING_VALUES()
    {
        magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent1", "prod1", 12.55, 11));
        magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Smakowy("producent2", "prod2", 22.55, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Tytoniowy("producent3", "prod3", 33, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));
        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 44, 11));

        //magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyBezFiltra("producent4", "prod4", 55, 11));

        //for (int i = 0; i < 10; i++)
        //{
        //    magazyny.GetAktualnyMagazyn().AddProduct(new PapierosyFiltr_Smakowy(i.ToString() + ". prod", "nazwa", 1.2 * i * i * i, i));
        //}

        Produkt tmp;
        foreach(Produkt prod in magazyny.GetAktualnyMagazyn().ToList)
        {
            tmp = (Produkt)prod.Clone();
            tmp.Ilosc = 1;
            klient.AddProduct(tmp);
        }


    }

}
public void DestroyProduct(string KodKreskowy)
{
    for (int i = 0; i < dictionary.Count; i++)
    {
        dictionary.Remove(KodKreskowy);
    }
}
Public ObservableCollection<Produkt> Produkts { get, set }  
Produkt p = Produkts.FirstOrDefault.Where(x => x.KodKreskowy = value);
if (p != null) Produkts.Remove(p);