WPF删除选定的行从treelistview

WPF删除选定的行从treelistview

问题描述:

我有下面的树列表视图:WPF删除选定的行从treelistview

<dxg:TreeListControl Name="treeList" Grid.Row="7" Height="230" Margin="10,2,10,0"> 
    <dxg:TreeListControl.Columns> 
     <dxg:TreeListColumn FieldName="Description" /> 
     <dxg:TreeListColumn FieldName="Package" /> 
     <dxg:TreeListColumn FieldName="Procedure" /> 
    </dxg:TreeListControl.Columns> 
    <dxg:TreeListControl.View> 
     <dxg:TreeListView Name="treeListView" KeyFieldName="Id" ParentFieldName="ParentId" /> 
    </dxg:TreeListControl.View> 
    <dxmvvm:Interaction.Behaviors> 
     <dxg:TreeListDragDropManager x:Name="dragDropManager" AllowDrag="True" /> 
    </dxmvvm:Interaction.Behaviors> 
</dxg:TreeListControl> 

我的问题是如何从树形列表视图中删除所选的行。 谢谢

+2

正确的做法是使用数据绑定并从基础数据源中删除项目。 – Dennis

+0

这就是我想要做的,但我不知道该怎么做。 –

+0

假设,'TreeListControl'是'ItemsControl',你需要绑定它的'ItemsSource'和一些'ObservableCollection',并从该集合中移除项目。如果你会发布代码,你会如何填充项目,并发布一个链接到'TreeListControl'描述。 – Dennis

看看How to: Manually Control Drag and Drop帮助文章,演示如何使用拖放相关事件自定义TreeList的拖放功能。

这篇文章的主要思想 - 如果你TreeListControl被绑定到一些集合:

treeList.ItemsSource = Stuff.GetStuff(); // ObservableCollection<Employee> 

可以使用TreeListDragDropManager.Drop事件来改变一些物品从这个集合(如删除的项目):

void TreeListDragDropManager_Drop(object sender, 
    var employees = ((ObservableCollection<Employee>)treelist.ItemsSource); 
    if(... some condition...){ 
     foreach (TreeListNode node in e.DraggedRows) { 
      var employee = node.Content as Employee; 
      employees.Remove(employee); 
    } 
} 

The complete sample project from the DevExpress Code Examples database.