繁体   English   中英

如何在Compact Framework c#中遍历BindingSource项目

[英]How can I iterate through a BindingSource items in Compact Framework 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
            }
        }

提前致谢。

问候里亚安

编辑:

杜德(Dude),我知道您在绘画方法上使用过绘画,这是我要执行的示例。 我从头开始创建所有内容,整个控件都是从代码中绘制的,并且我覆盖了所有需要的事件。 也许在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.

        }

如果有人需要删除该问题,则可以将其删除。 如果有其他人急于解决此问题并且不知道如何获得它,我将把它留在这里。 这些只是代码片段,无论如何都不能使用。

我只是将问题标记为已回答。 阅读我的编辑内容,以查看我愚蠢的思维错误以及为什么这很容易。

我将把职位留给那些可能会和我一样思考并且会毫不费力地奋斗的人,除非有人认为必须删除问题,否则您可以继续前进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM