我想将对象taskCollection
绑定到我的checkListBox
。 taskCollection
实现了接口IList<Task>
。
当我尝试将其用作数据源时,我收到错误
System.ArgumentException:复杂的DataBinding接受IList或IListSource作为数据源。
这是我的TaskCollection
:
using System;
using System.Collections.Generic;
using System.IO;
namespace CLBatchGUI
{
/// <summary>
/// Description of TaskCollection.
/// </summary>
public class TaskCollection:IList<Task>
{
private List<Task> _tasks;
public TaskCollection()
{
_tasks = new List<Task>();
}
public void executeTasks(){
}
public static void loadFromCSV(string path){
}
public void saveToCSV(string path){
StreamWriter sw= new StreamWriter(path);
saveToCSV(sw);
}
public void saveToCSV(TextWriter sw){
if(_tasks.Count==0)
throw new ApplicationException("There are no tasks to save");
foreach (var Task in _tasks) {
Task.validate();
}
try{
sw.Write("TaskName,");
for(int i=0;i<_tasks[0].job.parameters.Count;i++){
sw.Write(_tasks[0].job.parameters[i].Name);
if(i==_tasks.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
for(int i=0;i<_tasks.Count;i++){
sw.Write(_tasks[i].Name +",");
for(int n=0;n<_tasks[i].job.parameters.Count;n++){
sw.Write(_tasks[i].job.parameters[n].Text);
if(n==_tasks[i].job.parameters.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
}
}catch(Exception ex){
throw new ApplicationException(ex.Message);
}
}
public Task this[int index]{
get{
return _tasks[index];
}
set{
_tasks[index]=value;
}
}
public int Count {
get {
return _tasks.Count;
}
}
public bool IsReadOnly {
get {
return false;
}
}
public int IndexOf(Task item)
{
return _tasks.IndexOf(item);
}
public void Insert(int index, Task item)
{
_tasks.Insert(index,item);
}
public void RemoveAt(int index)
{
_tasks.RemoveAt(index);
}
public void Add(Task item)
{
_tasks.Add(item);
}
public void Clear()
{
_tasks.Clear();
}
public bool Contains(Task item)
{
return _tasks.Contains(item);
}
public void CopyTo(Task[] array, int arrayIndex)
{
_tasks.CopyTo(array,arrayIndex);
}
public bool Remove(Task item)
{
return _tasks.Remove(item);
}
public IEnumerator<Task> GetEnumerator()
{
return _tasks.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _tasks.GetEnumerator();
}
}
}
更新了课程:
using System;
using System.Collections.Generic;
using System.IO;
namespace CLBatchGUI
{
/// <summary>
/// Description of TaskCollection.
/// </summary>
public class TaskCollection:IList<Task>,IList
{
private List<Task> _tasks;
public TaskCollection()
{
_tasks= new List<Task>();
}
public void executeTasks(){
}
public static void loadFromCSV(string path){
}
public void saveToCSV(string path){
StreamWriter sw= new StreamWriter(path);
saveToCSV(sw);
}
public void saveToCSV(TextWriter sw){
if(_tasks.Count==0)
throw new ApplicationException("There are no tasks to save");
foreach (var Task in _tasks) {
Task.validate();
}
try{
sw.Write("TaskName,");
for(int i=0;i<_tasks[0].job.parameters.Count;i++){
sw.Write(_tasks[0].job.parameters[i].Name);
if(i==_tasks.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
for(int i=0;i<_tasks.Count;i++){
sw.Write(_tasks[i].Name +",");
for(int n=0;n<_tasks[i].job.parameters.Count;n++){
sw.Write(_tasks[i].job.parameters[n].Text);
if(n==_tasks[i].job.parameters.Count-1)
sw.Write("\n");
else
sw.Write(",");
}
}
}catch(Exception ex){
throw new ApplicationException(ex.Message);
}
}
public object this[int index]{
get{
return _tasks[index];
}
set{
_tasks[index]=value;
}
}
int IList.Count {
get {
return _tasks.Count;
}
}
bool IList.IsReadOnly {
get {
return false;
}
}
int IList.IndexOf(object item)
{
return _tasks.IndexOf(item);
}
void IList.Insert(int index, object item)
{
_tasks.Insert(index,item);
}
void IList.RemoveAt(int index)
{
_tasks.RemoveAt(index);
}
void IList.Add(object item)
{
_tasks.Add(item);
}
void IList.Clear()
{
_tasks.Clear();
}
bool IList.Contains(object item)
{
return _tasks.Contains(item);
}
void IList.CopyTo(object[] array, int arrayIndex)
{
_tasks.CopyTo(array,arrayIndex);
}
bool IList.Remove(object item)
{
return _tasks.Remove(item);
}
// IList.IEnumerator<object> GetEnumerator()
// {
// return _tasks.GetEnumerator();
// }
// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
// {
// return _tasks.GetEnumerator();
// }
}
}
答案 0 :(得分:3)
您的课程必须实施IList
或IListSource
,因为错误消息会告诉您。
您提出的课程实施IList<T>
,但不是IList
。
所以你必须自己实施IList
。您可以明确地执行此操作,因此您不能无意中添加object
不是Task
(因为非通用Add
方法将被隐藏),只要您不要我们明确地将您的TaskCollection
转换为IList
并调用非通用Add
方法。
public class TaskCollection : IList<Task>, IList
{
int IList.Add(object value)
{
// just call the generic version
Add((Task)value);
}
int IList.IndexOf(object value)
{
// just call the generic version
return IndexOf((Task)value);
}
...
}