开发者

AllowDrop for only some ListBoxItems

I have a list box representing the contents of a direcory. I want to allow dropping only on those items that represent directories themselves. I've tried two approaches:

First i set the itemsource of the listbox to a compositeCollection of all directory contents and tried to iterate through those with:

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

The messagebox comes up with '开发者_StackOverflow中文版Directory' or 'UserFile' for each item. I was hoping to have access to the items, check what they represent, and set AllowDrop as necessary.

My second approach was to add individual items like so:

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

but then it just comes up blank.


look at mdm20's answer wpf treeview blues. I want to select an item

Just make an allowDrop property and bind it appropriately.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜