开发者

controls that have items , wpf

开发者 https://www.devze.com 2023-02-14 18:12 出处:网络
I want to be able to get a control that i don\'t know in advance what type is it and to be able to go through it\'s items collection . The problem is that each control wants it\'s own casting and开发者

I want to be able to get a control that i don't know in advance what type is it and to be able to go through it's items collection . The problem is that each control wants it's own casting and开发者_如何学C i don't know how to do it generic...... if the control doesn't have items i want to use just this control.... Thanks.


This could work. A Control is a DependencyObject, and if that object inherits IEnumerable, it will have an iterator.

        private void DoStuffWithControl(DependencyObject dependencyObject)
    {
        if (dependencyObject is IEnumerable)
        {
            IEnumerator enumerator = (dependencyObject as IEnumerable).GetEnumerator();
            while (enumerator.MoveNext())
            {
                // Do whatever you want to do with the item (enumerator.Current)
            }
        }
        else
        {
            // Do whatever you want to do with the control
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号