This is a migrated thread and some comments may be shown as answers.

Cast TreeViewItem as Object

3 Answers 263 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 22 Feb 2011, 12:01 AM
Hi - I have a functioning tree view that is displaying a folder structure based collections of products, folders, and files.   When I navigate to  the file item, I would like to be able to double click the file and so be able to access the file object that was created for that item.  In my double click event however, I am unable to cast the tree view item as my file class:  I receive the error "Object reference not set to an instance of an object." for the line that attempts to cast the item as the file.  Is there an alternate way to get at my file object ?

private void radTreeView_DoubleClick(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
     
    RadTreeViewItem  selectedItem = e.OriginalSource as RadTreeViewItem;
    LogFile logfile = selectedItem.Item as LogFile;
 
        if (logfile != null)
        {
            MessageBox.Show("selected is file ");
            return;
        }
        return;
   }

Thanks!

Rich

3 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 24 Feb 2011, 01:43 PM
Hi Rich,

I cannot see something unusual from the code you provided. You could examine the attached sample and where the basic approach is realized, it's similar to yours. You could also send us a runnable sample and we could investigate in depth and advice you better.

Greetings,
Petar Mladenov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Rich
Top achievements
Rank 1
answered on 24 Feb 2011, 03:42 PM
Hi - thanks for the response. 
In your code you have an RadTreeView event handler of "ItemDoubleClick".   This generates an error in my visual studio:

Error   2   The property 'ItemDoubleClick' was not found in type 'RadTreeView'. 

Warning   1   The property 'ItemDoubleClick' does not exist in XML namespace 'http://schemas.telerik.com/2008/xaml/presentation'. Line 17 Position 44.   C:\Telerik Examples\Feb24TreeViewFromTelerikForum\TreeViewTestMainWindow.xaml   17   44   TreeViewTest

Do I need a reference to use this event handler?



0
Hristo
Telerik team
answered on 28 Feb 2011, 01:51 PM
Hi Rich,

It's very strange you are getting such an error. I'm able to download the sample and run it (double click over item) without exceptions. It is possible to have older version of our DLLs cached where the ItemDoubleClick event is not present.
Can you please try cleaning your project and deleting "bin" and "obj" directories, and check if it's going to work?

Also it would be great if you provide us a simple project (with DLLs you are using) in order to investigate the reason if the clean does not helps.

All the best,
Hristo
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
TreeView
Asked by
Rich
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Rich
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or