需要找出listBox内元素的类型,无论类型是按钮,单选按钮还是简单字符串。

以下是一些摘要:

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
                    {
                        if _item is of type of button
                            //DO this
                        else if _item is  typeof RadioButton
                            //Do that
                    }

最佳答案

这个怎么样:

foreach (var _item in listPhotoAlbum.Items)
{
    var radioButton = _item as RadioButton;

    if (radioButton != null)
    {
        //Do with radioButton
        continue;
    }

    var button = _item as Button;

    if (button != null)
    {
        //Do with button
        continue;
    }
}

关于c# - 如何检查ListBox中的项目类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3839895/

10-13 06:22