在c#中设置DataSource属性时,无法修改项集合

时间:2014-08-16 09:09:02

标签: c# facebook multithreading winforms data-binding

我正在使用对象数据源编写facebook win应用程序。 表单显示选择专辑中的照片, 并且对于在列表框中选择的每张照片,您都会在组框中看到照片详细信息。 在调试时,我被抛出"当设置了DataSource属性时,无法修改Items集合。"

这是我的代码:

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

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

1 个答案:

答案 0 :(得分: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"
}