How to Remove an Item from HierarchicalDataTemplate on a treeview

191 views Asked by At

I want to delete an item from an HierarchicalDataTemplate. I am using a data source. and due to that I can just delete an item from the treeview I need to remove it from it souce

what will be the logic to delete such a thing will appericiate some help.

attachig my code:enter image description here

WPF Code:

<TreeView.Resources>
                            <HierarchicalDataTemplate DataType="{x:Type cft:CommadStructure}" ItemsSource="{Binding Items}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                        <ColumnDefinition Width="10"></ColumnDefinition>
                                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Path=Index,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" />
                                    <TextBlock Text="{Binding Path=NameOfCommand,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="2"  />
                                </Grid>
                            </HierarchicalDataTemplate>
                        </TreeView.Resources>

C# try...:

private void RemoveOneItem_Click(object sender, RoutedEventArgs e)
    {
        TRVCommands.Items.Remove(TRVCommands.SelectedItem);

        string index = (TRVCommands.SelectedItem as ClassForTest.CommadStructure).Index;


        try
        {
            ObservableCollection<ClassForTest.CommadStructure> target = _OCommandStructure;

            for (int i = 0; i < target.Count; i++)
            {
                if (target[i].Index == index)
                {
                    target.Remove((TRVCommands.SelectedItem as ClassForTest.CommadStructure));
                    TRVCommands.Items.Refresh();
                    return;
                }
                ObservableCollection<ClassForTest.CommadStructure> NewTarget = _OCommandStructure[i].SubCommandStructure;
                NewTarget = target[i].SubCommandStructure;
                while (NewTarget.Count > 0)
                {
                    for (int j = 0; j < NewTarget.Count; j++)
                    {
                        if (NewTarget[i].Index == index)
                        {
                            NewTarget.Remove((TRVCommands.SelectedItem as ClassForTest.CommadStructure));
                            TRVCommands.Items.Refresh();
                            return;
                        }
                    }
                    NewTarget = NewTarget[]
                }



            }
        }
        catch
        {
            MessageBox.Show("There is an error on index.");
            return;
        }
    }
1

There are 1 answers

0
Barak Rosenfeld On

so the logic should be an recurse method that is checking via index/Name if this is the item and delete it for its item source.

C# code1:

 private void RecurseDeleteItem(string index, ObservableCollection<ClassForTest.CommadStructure> target ,ref bool foundAndDelete)
    {            
        for (int i = 0; i < target.Count; i++)
        {
            if (foundAndDelete)
            {
                return;
            }

            if (target[i].Index == index)
            {
                target.Remove(target[i]);
                TRVCommands.Items.Refresh();
                foundAndDelete = true;
                return;

            }
            

            if (target[i].SubCommandStructure.Count>0)
            {
                RecurseDeleteItem(index, target[i].SubCommandStructure, ref foundAndDelete);
                if (foundAndDelete)
                {
                    return;
                }
            }


        }
    }

C# Code2:

        string index = (TRVCommands.SelectedItem as ClassForTest.CommadStructure).Index;

        if ((TRVCommands.SelectedItem as ClassForTest.CommadStructure).StartLoop != "")
        {
            MessageBoxResult mbr = MessageBox.Show("You Asked to delete a loop, this will delete the entire commands inside the loop are you sure? ");
            if (mbr == MessageBoxResult.Yes)
            {

            }
            else
            {
                return;
            }
        }


        try
        {
            bool found = false;
            RecurseDeleteItem(index, _OCommandStructure, ref found);
        }
        catch
        {
            MessageBox.Show("There is an error on index.");
            return;
        }`