Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:16:11+00:00 2026-05-18T05:16:11+00:00

I am working on WPF TreeView s. I am able to add the new

  • 0

I am working on WPF TreeViews. I am able to add the new Items under tree, but I am not able to delete them from the list. In my code I am trying to get the index of the selected tree item and trying to Remove it. But the code is returning Index “-1”. This results in an ArgumentOutOfRangeException.

Please help to fix this.

<Window x:Class="MyTreeStructure.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="ADD" Height="23" HorizontalAlignment="Left" Margin="211,50,0,0"
                Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TreeView Height="200" HorizontalAlignment="Left" Margin="27,12,0,0" Name="treeView1"
                  VerticalAlignment="Top" Width="120" >
            <TreeViewItem Name="Parent" Header="My Tree Content">
            </TreeViewItem>
        </TreeView>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="211,12,0,0" Name="textBox1"
                 VerticalAlignment="Top" Width="120" />
        <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="211,79,0,0"
                Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</Window>

namespace MyTreeStructure
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem temp = new TreeViewItem();
            temp.Header = textBox1.Text;
            Parent.Items.Add(temp);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = treeView1.SelectedItem.ToString();
            Parent.Items.RemoveAt(treeView1.Items.IndexOf(treeView1.SelectedItem));        
            **// Here I am getting exception. What should be the code here ??**             
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:16:12+00:00Added an answer on May 18, 2026 at 5:16 am

    below line is having the problem

    treeView1.Items.IndexOf(treeView1.SelectedItem))
    

    Since treeview1 is only having one node ‘Parent’ , rest of the node which you have added is in the node called ‘Parent’.

    So if you trying to get the index for a node in treeView1.Items it will return -1 except for the node called ‘Parent’ for which it will return 0.

    so you nned to modify the code for removing a node as below.

     private void button2_Click(object sender, RoutedEventArgs e)     
            {     
                textBox1.Text = treeView1.SelectedItem.ToString();   
                int index = treeView1.Items.IndexOf(treeView1.SelectedItem));   
                 if(index < 0)
                   {
                       index  = Parent.Items.IndexOf(treeView1.SelectedItem));
                   }    
    
                if(index > 0)
                  {
                       Parent.Items.RemoveAt(index);
                  } 
    
            }     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a WPF application which has a treeview that represents an
I'm trying to display a Wpf Treeview with items sorted by a CollectionViewSource. Currently,
I'm working on a WPF application that sometimes exhibits odd problems and appears to
I'm working on a WPF application and is using the Model-View-ViewModel pattern. The application
I am working on a graph in WPF, in each section there are thousands
I have a WPF form and I am working with databinding. I get the
I'm working through the Data Binding chapter in Pro WPF in C# 2008. They
Been trying to find a working implementation of a WPF listview (or listbox) where
I'm working on a simple application to start learning my way around WPF. I
I'm creating an WPF app, so I'm mostly working with the ImageSource class for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.