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

Binding to SelectedItem

2 Answers 107 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Franziska
Top achievements
Rank 1
Franziska asked on 07 Dec 2009, 10:37 AM
Hello

The SelectedItem property of the treeview does not always contain what I would expect.
Examples:
  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,
Franziska

2 Answers, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 10 Dec 2009, 11:52 AM
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.
0
Franziska
Top achievements
Rank 1
answered on 10 Dec 2009, 01:47 PM
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,
Franziska
Tags
TreeView
Asked by
Franziska
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Franziska
Top achievements
Rank 1
Share this question
or