具有项目的控件,wpf

时间:2011-03-03 09:33:52

标签: wpf-controls

我希望能够获得一个我事先不知道它的类型的控件,并能够通过它的项目集合。问题是,每个控件都想要它自己的转换,我不知道怎么做通用......如果控件没有项目我只想使用这个控件.... 感谢。

1 个答案:

答案 0 :(得分:0)

这可行。 Control是DependencyObject,如果该对象继承IEnumerable,它将有一个迭代器。

        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
        }
    }