I am facing a problem when I add data in ListView. The problem is that ListView 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
<ListView HorizontalAlignment="Left" Name="invoiceLV" Height="207" Margin="154,88,-752,0" VerticalAlignment="Top" Width="914">
<ListView.View>
<GridView>
<GridViewColumn Header="Code" DisplayMemberBinding="{Binding ProductCode}" Width="150" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding ProductName }" Width="150 " />
<GridViewColumn Header=" Units" DisplayMemberBinding="{Binding Unit}" Width="150" />
<GridViewColumn Header=" Unit Price" DisplayMemberBinding="{Binding UnitPrice}" Width="120" />
<GridViewColumn Header=" Size(gm)" DisplayMemberBinding="{Binding Size}" Width="120" />
<GridViewColumn Header=" GST %" DisplayMemberBinding="{Binding GST}" Width="100" />
<GridViewColumn Header=" Price" DisplayMemberBinding="{Binding Price}" Width="120" />
</GridView>
</ListView.View>
</ListView>
public partial class Invoice : UserControl
{
private void Button_Click(object sender, RoutedEventArgs e)
{
inv.calculation();
invoiceLV.Items.Add(inv);
}
}
And Binding class is:
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));
}
}
}
}
And the screenshots are given below.
Image after one record
Image after two records
[
2
