AllowDrop只有一些ListBoxItems

问题描述:

我有一个列表框表示一个direcory的内容。我想只允许放在代表目录本身的项目上。我已经试过两种方法:AllowDrop只有一些ListBoxItems

首先我ListBox的的ItemSource设置的所有目录内容compositeCollection并试图通过那些迭代:

foreach (ListBoxItem lbItem in directoryExplorer.Items) 
{ 
    MessageBox.Show(lbItem.DataContext.GetType().ToString()); 
} 

的消息框出现与“目录”或每个项目的'UserFile'。我希望能够访问这些项目,检查它们代表的内容,并根据需要设置AllowDrop。

我的第二个办法是增加单个项目,像这样:

ListBoxItem的NXT;

foreach (Directory d in dir.childdirs) 
{ 
    MessageBox.Show(d.name); 
    nxt = new ListBoxItem(); 
    nxt.DataContext = d; 
    nxt.AllowDrop = true; 
    nxt.Name = d.name; 
    directoryExplorer.Items.Add(nxt); 
} 
foreach (UserFile f in dir.childfiles) 
{ 
    MessageBox.Show(f.name); 
    nxt = new ListBoxItem(); 
    nxt.AllowDrop = false; 
    nxt.DataContext = f; 
    nxt.Name = f.name; 
    directoryExplorer.Items.Add(nxt); 
} 

但后来它只是空白。

+0

你需要在你的列表框中放置物品吗? – 2011-06-13 11:48:55

看看mdm20的回答wpf treeview blues. I want to select an item

只是要一个AllowDrop属性并适当地绑定。