Search code examples
c#wpfxamltreeview

Multiple selection inside a TreeView


I want to mark several things and delete them with "Delete" button at key-board so that I do not have to delete everything one by one

  • Several units can be marked with "Ctrl + Click" or "Ctrl + ArrowDown"(usual windows keys)
  • All marked Units can be deleted with Click on "Dlelte" button on key-board

The part for marking several units is the one I can not achieve. Deleting part should not be hard.

At the moment tree is looking like this:

<TreeView  Name="plantsTree" Background="{StaticResource PlantConfiguratorBackground}" VerticalAlignment="Stretch" ItemsSource="{Binding PlantTrees}" Grid.Column="0"
             KeyUp="plantsTree_KeyUp" Margin="0,0,5,0" SelectedItemChanged="plantsTree_SelectedItemChanged">
            <TreeView.Resources>
                <HierarchicalDataTemplate x:Key="dtBaseTree" ItemsSource="{Binding Path=SubItems, Converter={x:Static local:Converters.OrderByIdx}}"  >
                    <StackPanel Orientation="Horizontal">
                            <Image Width="18" Height="18" Source="{Binding Path=ConfigIcon}"/>
                            <TextBlock Text=" "/>
                            <local:TextBoxWithCommandBoundContextMenu /> //this is just a textbox
                    </StackPanel>
                </HierarchicalDataTemplate> 

                //there are 9 HierarchicalDataTemplates like this one above... 
                <HierarchicalDataTemplate>...</HierarchicalDataTemplate>
            </TreeView.Resources>

           <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="False"/>
                    <EventSetter Event="RequestBringIntoView" Handler="TreeViewItem_RequestBringIntoView"/>
                    <EventSetter Event="PreviewMouseDown" Handler="SelectionChange"/>
                </Style>
            </TreeView.ItemContainerStyle>

Basically I want to be able with Ctrl + Click to select many elements in treeview. Is this passible.

It is passible with ListView. But not sure about TreeView. If not can this be somehow represented as a ListView.


Solution

  • Answer to my question I found here, source code is availible on github or it can be used as a nuget package: https://unclassified.software/en/source/multiselecttreeview