How to dynamically add items in ListView in WPF
<p>I am facing a problem when I add data in <code>ListView</code>. The problem is that <code>ListView</code> add data first time correctly but the second-time data is added it replaces the previous row with new data. Here is my list view XAML and XAMl.cs</p>

<pre><code>&lt;ListView HorizontalAlignment="Left" Name="invoiceLV" Height="207" Margin="154,88,-752,0" VerticalAlignment="Top" Width="914"&gt;
    &lt;ListView.View&gt;
        &lt;GridView&gt;
            &lt;GridViewColumn Header="Code" DisplayMemberBinding="{Binding ProductCode}" Width="150" /&gt;
            &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding ProductName }" Width="150 " /&gt;
            &lt;GridViewColumn Header=" Units" DisplayMemberBinding="{Binding Unit}" Width="150"  /&gt;
            &lt;GridViewColumn Header=" Unit Price" DisplayMemberBinding="{Binding UnitPrice}" Width="120"  /&gt;
            &lt;GridViewColumn Header=" Size(gm)" DisplayMemberBinding="{Binding Size}" Width="120"  /&gt;
            &lt;GridViewColumn Header=" GST %" DisplayMemberBinding="{Binding GST}" Width="100"  /&gt;
            &lt;GridViewColumn Header=" Price" DisplayMemberBinding="{Binding Price}" Width="120"  /&gt;
        &lt;/GridView&gt;
    &lt;/ListView.View&gt;
&lt;/ListView&gt;

public partial class Invoice : UserControl
{
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        inv.calculation();
        invoiceLV.Items.Add(inv);
    }
}
</code></pre>

<p>And Binding class is:</p>

<pre><code>class Invoice : INotifyPropertyChanged
{
    string productcode;
    string productname;
    double unitprice;
    int size;
    double gst;
    double price;
    string code;
    string name;
    int unit;

    public double Price
    {
        get { return price; }
        set
        {
            if (price != value)
            {
                price = value;
                OnPropertyChanged("Price");
            }
        }
    }

    public double GST
    {
        get { return gst; }
        set
        {
            if (gst != value)
            {
                gst = value;
                OnPropertyChanged("GST");
            }
        }
    }

    public int Size
    {
        get { return size; }
        set
        {
            if (size != value)
            {
                size = value;
                OnPropertyChanged("Size");
            }
        }
    }

    public double UnitPrice
    {
        get { return unitprice; }
        set
        {
            if (unitprice != value)
            {
                unitprice = value;
                OnPropertyChanged("UnitPrice");
            }
        }
    }

    public string ProductName
    {
        get { return productname; }
        set
        {
            if (productname != value)
            {
                productname = value;
                OnPropertyChanged("ProductName");
            }
        }
    }

    public string ProductCode
    {
        get { return productcode; }
        set
        {
            if (productcode != value)
            {
                productcode = value;
                OnPropertyChanged("ProductCode");
            }
        }
    }

    public string Code
    {
        get { return code; }
        set
        {
            if (code != value)
            {
                code = value;
                OnPropertyChanged("Code");
            }
        }
    }

    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public int Unit
    {
        get { return unit; }
        set
        {
            if (unit!= value)
            {
                unit= value;
                OnPropertyChanged("Unit");
            }
        }
    }

    public void calculation()
    {
        Bussiness.Invoice i = new Bussiness.Invoice();
        ProductName = i.Name(this);
        ProductCode = code;
        Unit = Unit;
        UnitPrice = i.Unit(this);
        GST = 18;
        Size = i.Size(this);
        Price =unit* (UnitPrice + (UnitPrice * (GST / 100)));

    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

}
</code></pre>

<p>}</p>

<p>And the screenshots are given below.</p>

<p><strong>Image after one record</strong></p>

<p><a href="https://i.stack.imgur.com/jSjzx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jSjzx.png" alt="Image after one record"></a></p>

<p><strong>Image after two records</strong></p>

<p>[<img src="https://i.stack.imgur.com/fzoPG.png" alt="image after two records"><a href="https://i.stack.imgur.com/fzoPG.png" rel="nofollow noreferrer">2</a></p>