【问题标题】:Items collection cannot be modified when the DataSource property is set in c#在c#中设置DataSource属性时无法修改Items集合
【发布时间】:2014-10-09 21:50:11
【问题描述】:

我正在使用对象数据源编写一个 facebook win 应用程序。 表格显示所选相册中的照片, 对于列表框中选择的每张照片,您会在组框中看到照片的详细信息。 调试时抛出“设置 DataSource 属性时无法修改项目集合。”

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Facebook;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace FacebookWinApp
{
    public class FormAlbumPictures : Form
    {
        private Label labelChoosePicture;
        private ListBox listBoxPictures;
        private PictureBox pictureBoxPictureFromAlbum;
        private Button buttonOK;
        private Button buttonCancel;
        private BindingSource photoBindingSource;
        private IContainer components;
        private GroupBox groupBoxPhotoDetails;
        private Label labelCurrentPhotoPlaceName;
        private DataGridView dataGridViewCurrentPhotoTags;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
        private BindingSource tagsBindingSource;
        private Label labelCurrentPhotoCreatedTime;
        private Label labelCurrentPhotoHeight;
        private Label labelCurrentPhotoName;
        private Label labelCurrentPhotoUpdateTime;
        private Label labelCurrentPhotoWidth;
        private Photo m_ChoosenPhoto = null;

        public Photo ChoosenPhoto
        {
            get { return m_ChoosenPhoto; }
        }

        public User UserLoggedIn { get; set; }

        public Album AlbumName { get; set; }

        public FormAlbumPictures()
        {
            this.InitializeComponent();
        }

        public void FetchPhotosFromAlbum()
        {
            var allPhotosFromAlbum = AlbumName.Photos;

            if (!listBoxPictures.InvokeRequired)
            {
                photoBindingSource.DataSource = allPhotosFromAlbum;
            }
                else
            {
                listBoxPictures.Invoke(new Action(() => photoBindingSource.DataSource = allPhotosFromAlbum));
            }
            listBoxPictures.Invoke(new Action(() =>
                {
                    listBoxPictures.DisplayMember = "UpdateTime";
                    foreach (Photo photo in AlbumName.Photos)
                    {
                        listBoxPictures.Items.Add(photo); /// Here the exception thrown
                    }
                }));
        }

        private void listBoxPictures_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySelectedPhoto();
        }

        private void displaySelectedPhoto()
        {
            Photo selectedPhoto = null;

            if (listBoxPictures.SelectedItems.Count == 1)
            {
                selectedPhoto = listBoxPictures.SelectedItem as Photo;
                if (selectedPhoto.PictureNormalURL != null)
                {
                    pictureBoxPictureFromAlbum.LoadAsync(selectedPhoto.PictureNormalURL);
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (listBoxPictures.SelectedItem != null)
            {
                m_ChoosenPhoto = listBoxPictures.SelectedItem as Photo;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}

我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: c# facebook multithreading winforms data-binding


    【解决方案1】:

    答案本身就有问题:设置 DataSource 属性时无法修改项目集合。如果您需要添加一些东西,那么您应该添加到DataSource,而不是直接添加到ListBox.Items 集合中。

    假设listBoxPictures 绑定到photoBindingSource。您可以执行以下操作。

    public void FetchPhotosFromAlbum()
    {
        if (listBoxPictures.InvokeRequired)
        {
           listBoxPictures.Invoke(new Action(FetchPhotosFromAlbum));
           return;
        }
    
         var allPhotosFromAlbum = AlbumName.Photos;
         photoBindingSource.DataSource = allPhotosFromAlbum;
         listBoxPictures.DisplayMember = "UpdateTime"
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2017-02-23
      • 2020-12-24
      相关资源
      最近更新 更多