2013-04-26 67 views
2

我希望有某个地方能够帮助我解决这个问题。我如何迭代通过Compact Framework中的BindingSource项目c#

我正在为windows ce移动设备创建自定义控件。我创建了一个属性,以便用户可以将任何源代码绑定到我的控件。我需要遍历源代码并获取显示成员路径的项目值。

我声明我的来源,然后创建属性。在构造中,如果源发生更改,我会注册一个事件,以便我可以再次开始绘制对象。这是我第一次从这个角度处理绑定源码。我错过了什么或使用它错了吗?这里是一些代码片段

private BindingSource _bindingCollection; 

public object DataSource 
     { 
      set { _bindingCollection.DataSource = value; } 
     } 
public string DisplayMemberPath { get; set; } 

ctor() 
{ 
_bindingCollection.DataSourceChanged += _bindingCollection_DataSourceChanged; 
} 


void _bindingCollection_DataSourceChanged(object sender, EventArgs e) 

     { 
      foreach (var item in _bindingCollection) 
      { 
       //I am stuck here on getting the value from the item that is 
       // specified by DisplayMemberPath to paint my objects on the control 
       //I can only paint on the control and cannot add any controls to it. 
       //So here a method is called with the value as a parameter and the 
       //Graphics object will draw the string 
      } 
     } 

谢谢先进。

问候 里安

编辑:

哥们,我知道您使用paint方法来作画,这是我想要做的一个例子。我从头开始创建所有的东西,整个控件是从代码中提取的,我覆盖了所有需要的事件。也许在DisplayMemberPath中设置_bindingCollection.DataMember?那么当迭代源代码时,它会给出这个项目吗?现在测试并发布答案,如果它有效。请不要更多的评论或回答告诉我使用油漆或类似的东西。专注于从集合中获得显示成员价值的问题:)

编辑:找到答案 好吧,对不起,这实际上是一个非常愚蠢的问题,我在忙于很多事情时都会问。这实际上很容易获得财产价值。

 for (int index = 0; index < _bindingCollection.Count; index++) 
     { 
      var propertyValue = _bindingCollection[index].GetType().GetProperty(DisplayMemberPath).GetValue(_bindingCollection[index], null); 
      // Can do anthing now with the value here or just create a 
      //method of returning this value with this line of code on top. 

     } 

如果有人需要删除它,可以删除该问题。如果有其他人急于解决这个问题并且不知道如何得到它,我会把它留在这里。这些只是代码片段,而不是可以使用的方法。

+0

你不应该在DataSourceChanged中画东西,你需要使用Paint事件。 – 2013-04-26 06:29:47

回答

1

我只是将问题标记为已回答。阅读我的编辑,看看我愚蠢的思想错误,为什么它很容易。

我会离开这个岗位的人,可能会去和我一样思考,并非常努力,除非有人觉得这个问题必须删除,你可以继续前进。