将项添加到radlistbox

时间:2012-12-07 17:31:50

标签: c# asp.net telerik

我正在尝试将FileInfo中的项添加到我的RadListBox中,虽然我无法,但我尝试将文件转换为RadListBoxItem对象,但是我得到的错误是它无法将字符串转换为radlistboxitem。有人可以放一点光吗?感谢。

                DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(fp));
                lb_Files.Items.Clear();
                foreach (FileInfo file in dir.GetFiles())
                {
                    RadListBoxItem rlb = new RadListBoxItem();
                    rlb = (RadListBoxItem)file.ToString();
                    //radListBox
                    lb_Files.Items.Add(rlb.ToString());
                }

2 个答案:

答案 0 :(得分:2)

试试这个

DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(fp));
lb_Files.Items.Clear();
foreach (FileInfo file in dir.GetFiles())
{
   lb_Files.Items.Add(new RadListBoxItem(file.ToString(), file.ToString()));
}

答案 1 :(得分:1)

不能将String对象转换为RadListBoxItem,您必须使用该字符串创建RadListBoxItem作为您的值和文本属性:

所以替换这个:

 RadListBoxItem rlb = new RadListBoxItem();
 rlb = (RadListBoxItem)file.ToString();
 //radListBox
 lb_Files.Items.Add(rlb.ToString());

有了这个:

lb_Files.Items.Add(new RadListBoxItem
{
     Value = file.ToString(),
     Text = file.ToString()
});