C#WPF将项目添加到listview会不断覆盖现有数据

时间:2017-08-04 14:19:45

标签: c# wpf forms listview observablecollection

我是C#的新手。我有一个MainWindow,其中包含ListView(lvStores),此外,我还有一个按钮,可以打开另一个窗口(NewStoreWindow)。 NewStoreWindow包含2个文本框和一个“确定”按钮。当我在文本框中写入内容并按“确定”时,窗口正在关闭并添加到我的ListView。但是,当我尝试向ListView添加新项目时,在添加第一项后,它只会覆盖它。或者实际上,它将删除它,然后它将添加下一个。我认为这与我的ObservableCollection有关。我试图将它们放在我的public MainWindow()中但是代码不会执行。此外,我尝试使用List代替ObservableCollection

MainWindow代码片段

private void cmdNewStore_Click(object sender, RoutedEventArgs e)
        {
            if (m_blnBusy)
            {
                return;
            }

            // Open NewStoreWindow in a new window
            NewStoreWindow newStore = new NewStoreWindow();
            newStore.ShowDialog();

            ObservableCollection<AddListViewItem> lvColumnList = new ObservableCollection<AddListViewItem>();
            ObservableCollection<AddListViewItem> items = (ObservableCollection<AddListViewItem>)lvStores.ItemsSource;
            lvStores.ItemsSource = lvColumnList;

            switch (SLvCol)
            {
                // If CSV is NOT loaded on start up and listview is then empty
                case false:

                    // If no items exist in listview
                    if (items == null)
                    {
                        // Add item from textbox from NewStoreWindow
                        lvColumnList.Add(new AddListViewItem() {sID = SNewStoreTbid, sName = SNewStoreTbName});
                        lvStores.ItemsSource = lvColumnList;

                        // Refresh listview
                        lvStores.Items.Refresh();
                    }
                    else // If items exists
                    {
                        if (items != null)
                        {
                            bool itemAlreadyAdded = false; // use for boolean checking
                            foreach (AddListViewItem item in items) // loop through all items in listview
                            {
                                //Do some work with the item
                                MessageBox.Show(item.sID);

                                if (item.sID == SNewStoreTbid) // Check if new item already exists
                                {
                                    itemAlreadyAdded = true;
                                    break;
                                }

                                if (itemAlreadyAdded) // Show messagebox if it exists
                                {
                                    MessageBox.Show("ID needs to be unique, please respecify!", "Duplicate ID",
                                        MessageBoxButton.OK, MessageBoxImage.Error);
                                }
                                else // if it does not exist, create it from the textbox values
                                {
                                    lvColumnList.Add(
                                        new AddListViewItem() {sID = SNewStoreTbid, sName = SNewStoreTbName});
                                    lvStores.ItemsSource = lvColumnList;

                                    // refresh listview
                                    //ICollectionView view = CollectionViewSource.GetDefaultView((List<AddListViewItem>)lvStores.ItemsSource);
                                    //view.Refresh();
                                    lvStores.Items.Refresh();
                                }
                            }
                        }
                    }
                    break;
 .....
        }

// Bind textbox values from NewStoreWindow
public class AddListViewItem
{
    public string sID { get; set; }
    public string sName { get; set; }
}
AddListViewItem item = new AddListViewItem
{
    sID = SNewStoreTbid, //SNewStoreTbid,
    sName = SNewStoreTbName //SNewStoreTbName
};

0 个答案:

没有答案
相关问题