繁体   English   中英

如何在WPF中的ListView中动态添加项目

[英]How to dynamically add items in ListView in WPF

我在ListView添加数据时遇到问题。 问题是ListView第一次正确添加数据,但是第二次添加数据,它用新数据替换了前一行。 这是我的列表视图XAML和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);
    }
}

绑定类是:

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));
        }
    }

}

}

屏幕截图如下。

一张记录后的图片

一张记录后的图片

两次记录后的图像

[ 两次记录后的图像 2

理想情况下,您应该学习MVVM并将您的收藏集绑定到列表视图。 但是,要解决此问题,您要使用相同的Invoice实例一次又一次地添加。 创建新实例,然后添加。

private void Button_Click(object sender, RoutedEventArgs e)
{
    var inv = new Invoice();
    inv.calculation();
    invoiceLV.Items.Add(inv);


}

这是因为每次您按下按钮时,您都会用不同的值替换相同的inv实例。 MVVM的方法是设置它的itemsource并将其绑定到一个集合。

 ObservableCollection<Invoice> _yourItemSource= new ObservableCollection<Invoice>(); 
 invoiceLV.ItemsSource = _yourItemSource;


  private void Button_Click(object sender, RoutedEventArgs e)
  {

         var inv = new Invoice();
         inv.calculation();
         _yourItemSource.Add(inv);

  }
private void Button_Click(object sender, RoutedEventArgs e)
{
      var invoice = new Invoice();
      invoice.calculation();
      invoiceLV.Items.Add(invoice);
  //Add items to your listview by above code!
}

您也可以将数据绑定为相同的方式!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM