Binding to SelectedItem

3 posts, 0 answers
  1. Franziska
    Franziska avatar
    29 posts
    Member since:
    Dec 2009

    Posted 07 Dec 2009 Link to this post


    The SelectedItem property of the treeview does not always contain what I would expect.
    1. Cancel Selection
      The ItemsSource property is bound to a collection containing A and B objects. I want that only the A object are selectable. The B objects are just for information. I use the following code:
      void radTreeView_PreviewSelected(object sender, Telerik.Windows.RadRoutedEventArgs e) 
        // get a reference to the item that will be selected   
        RadTreeViewItem itemToSelect = e.OriginalSource as RadTreeViewItem;   
        // check item that will be selected   
        if (itemToSelect != null && itemToSelect.Item is B) 
          e.Handled = true
      However, the SelectedItem property contains the B object afterwards. I would expect the SelectedItem property to be null.
    2. Remove the selected item from the bound collection
      If I remove the currently selected item from the bound collection, then the node has disappeard as I expected, but the SelectedItem property still contains the removed object. I would expect, that the SelectedItem property always contains objects that are also in the ItemsCollection or null otherwise.

    How should I handle these use cases? Binding to another property?

    Best regards,
  2. Kiril Stanoev
    Kiril Stanoev avatar
    1508 posts

    Posted 10 Dec 2009 Link to this post

    Hello Franziska,

    First of all, I'd like to apologize for the late reply.

    I investigated both issues you describe and indeed, even though PreviewSelected is handled, the SelectedItem holds a B object. We will further investigate the problem and provide solution for it as soon as possible. For that, I have added 1000 Telerik points to your account.

    As for the second issue you describe, unfortunately I was not able to reproduce it. I bound a TreeView to a collection of objects. As soon as I removed the SelectedItem of the TreeView from the collection, the SelectedItem was null. I have attached a sample project that demonstrates this functionality. Have a look at it and let me know if I am missing something.

    Sincerely yours,
    Kiril Stanoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft R3 2016 release webinar banner
  4. Franziska
    Franziska avatar
    29 posts
    Member since:
    Dec 2009

    Posted 10 Dec 2009 Link to this post

    Hello Kiril

    Thank you very much for your reply.
    I have looked at your example for the second issue. It was very helpful, because I have seen, that I have done something wrong in my internal data structure. Sorry for causing trouble with this issue.

    Kind regards,
Back to Top