9

我正在编写一个 Windows 应用程序并使用 Listbox 控件。我正在使用 Visual Studio C# 2008 Express Edition 进行开发。

我有一个看起来像这样的数据对象

public class RootObject
{
   public List<SubObject> MySubObjects{ get; set;}
}

我的表单上有一个 ListBox,还有一个属性“MyRootObject”,显然它包含一个 RootObject。当控件初始化时,我设置:

_listBox.DataSource = MyRootObject.MySubObjects;

现在,当表单加载时,我调试并看到 DataSource 设置正确。但什么都没有显示。我已经覆盖了 SubObject 的 ToString() 方法,它甚至没有被调用。我尝试将 _listBox.DisplayMember 设置为 SubObject 的属性,以查看那里是否存在问题,但仍然没有。在设置数据源后,我尝试调用 _listBox.Update() 和 _listBox.Refresh(),但仍然没有爱。DataSource 拥有所有数据......它只是拒绝显示它。

所以在调试时,我想知道WTF,我决定就这样做

_listBox.DataSource = new List<SubObject>{ new SubObject(), new SubObject() };

果然,这行得通,我看到我的列表框中列出了两件事。

所以,真的很好奇,我决定尝试复制对象列表并将其放入列表框中,如下所示:

_listBox.DataSource = MyRootObject.MySubObjects.ToArray();

这行得通!这是我现在的问题的一种解决方法……但非常烦人。有谁知道为什么我需要基本上复制这样的对象列表才能使其工作,而不仅仅是设置 _listBox.DataSource = MyRootObject.MySubObjects; ? 同样,DataSource 在设置后无论哪种方式都具有所有正确的数据......它只是在复制数据时,它实际显示,而当它不是时,它不会显示。

4

8 回答 8

5
((CurrencyManager)_listBox.BindingContext[_listBox.DataSource]).Refresh();

Sux0r 我知道,但这有效。(最初在这里找到答案)

于 2009-05-20T00:51:42.400 回答
1

在我的脑海中,这是因为ListBox.DataSource属性必须包含实现IList接口的东西。你的泛型List<SubObject>没有实现IList;它实现IList<T>(在System.Collections.Generic命名空间中)。Array另一方面,对象确实继承自IList,因此通过这种对象传递数据是可行的。

您可以尝试从您的对象中拉出一个Enumerator(也实现IListList<SubObject>并将其插入。如果它有效,那么我描述的问题就是您的问题。如果没有,那我就是在胡说八道。

如果这确实是问题,我很惊讶推入不受支持的对象不会引发异常。

于 2009-03-24T03:57:55.177 回答
1

到目前为止,我知道,每当您想将集合设置为

[ComboBox,ListBox].DataSource

您必须设置 DisplayMember 和 ValueMember。DisplayMember 和 ValueMember 填充了集合中分配给列表框/组合框的类的属性名称。前任。

//Populate the data
RootObject root = new RootObject();
root.MySubObjects.Add(new SubObject("1", "data 1"));
root.MySubObjects.Add(new SubObject("2", "data 2"));

//Assign data to the data source
_listBox.DisplayMember = "DisplayProperty";
_listBox.ValueMember = "ValueProperty";
_listBox.DataSource = root.MySubObjects;

root.MySubObjects 返回子对象列表,子对象必须具有名为 DisplayProperty 和 ValueProperty 的属性,例如。

public class RootObject
{
     public List<SubObject> MySubObjects { get; set; }
}

public class SubObject
{
     public string ValueProperty { get; set; }
     public string DisplayProperty { get; set; }
}
于 2009-03-31T08:14:41.617 回答
0

I have a special rule for problems like this that I always try to remember before wasting an entire day hammering on it (believe me, I've spent many days hammering!). The rule is: when system behavior is really strange, the underlying cause must be very stupid. As intelligent programmers, we have a tendancy to search for esoteric causes or bugs in framework code to explain our problems. Sometimes the answer is that we were in a hurry and made a careless mistake that we haven't yet caught.

To quote Sherlock Holmes, "when you've removed the impossible, then whatever remains, no matter how improbable, must be the truth". In this case, the improbable truth is that the MySubObjects property of MyRoot object is null.

于 2009-04-05T05:57:10.810 回答
0

你可以试试

_listBox.DataSource = new BindingList<SubObject> (MyRootObject.MySubObjects);

反而。BindingList 实现了比 List 更多的接口,这对于 DataBinding 是必不可少的。

于 2009-04-02T21:20:52.207 回答
0

you could try and use a BindingSource ( http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx )

inbetween the Listbox and your collection. Bindingsources handles a bunch of stuff and also includes Suspend/ResumeBinding properies that can be useful when updating the list

you could also try out wpf as its databinding is far superior to that of winforms :) but maybe thats not possible in your case

于 2009-03-27T01:39:48.700 回答
0

I think you have to call Bind method after assigning to the list box data source something like _listBox.DataSource.bind() and you will have your listbox disappeared

于 2009-03-24T07:34:17.200 回答
0

我相信你需要调用 _listbox.DataBind(); 分配数据源后。但是,我以前从未使用过属性作为数据源,我只使用过方法。您是否尝试过将您的属性更改为一种方法以查看是否存在问题?

于 2009-03-28T16:50:58.593 回答