Cast TreeViewItem as Object

4 posts, 0 answers
  1. Rich
    Rich avatar
    3 posts
    Member since:
    Feb 2011

    Posted 21 Feb 2011 Link to this post

    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
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 24 Feb 2011 Link to this post

    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!
  3. UI for WPF is Visual Studio 2017 Ready
  4. Rich
    Rich avatar
    3 posts
    Member since:
    Feb 2011

    Posted 24 Feb 2011 Link to this post

    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?



  5. Hristo
    Admin
    Hristo avatar
    352 posts

    Posted 28 Feb 2011 Link to this post

    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!
Back to Top