【问题标题】:AllowDrop for only some ListBoxItemsAllowDrop 仅适用于某些 ListBoxItems
【发布时间】:2011-07-17 23:20:48
【问题描述】:

我有一个表示目录内容的列表框。我想只允许删除那些代表目录本身的项目。我尝试了两种方法:

首先,我将列表框的 itemsource 设置为所有目录内容的组合集合,并尝试通过以下方式迭代:

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

消息框为每个项目提供“目录”或“用户文件”。我希望能够访问这些项目,检查它们代表什么,并根据需要设置 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);
}

但它只是空白。

【问题讨论】:

  • 您需要将项目放入列表框中吗?

标签: c# wpf listboxitem


【解决方案1】:

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

只需创建一个 allowDrop 属性并适当地绑定它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 2018-11-09
    • 2012-10-30
    • 2020-10-21
    • 2019-04-11
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多