Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Setting the TreeView "HasItems" Property when using a ObservableCollection
6 replies. Latest Post by alaskanrogue on November 15, 2009.
(0)
alaskanr...
Member
19 points
67 Posts
11-06-2009 4:20 PM |
I assumed that building a heirarchical observablecollection using a class derived from ObservableCollection (i.e. public class productcategory : ObservableCollection<productcategory>) would be traversable by a treeview if the binding in the HeirarchicalDataTemplate is set to the Items property (i.e. ItemsSource="{Binding Items}".
It appears that the TreeViewItem class has a readonly property "HasItems" property. Is the traversal activated by that property? See the bottom of the screenshot below. It is set to false. As you can see in the second screenshot, the item is aware of the observablecollection which was added and has three items, each of which is another observablecollection. If the treeview's itemssource is a observablecollection, how is that property set? Why don't I see a heirarchy of TreeViewIItems?
Maybe I am just grasping for why my treeview is not expandable...
K2P2
Participant
1028 points
337 Posts
11-06-2009 6:52 PM |
I'm just wondering if some of these properties only get set after the control loads into its parent, not initialized but loaded.
I think you're looking were you initialize. Try looking later, in some other event.
11-07-2009 5:39 AM |
Thanks for replying.
As the control exists in XAML and I can see it in the SL app with the top level items, I would think it was loaded. I would have thought it would get set after the itemssources was set and the observablecollection was attached.
11-07-2009 1:34 PM |
I guess I should have said "after the event that you are in". I think it may need to finish this event, go back to the ui thread and, maybe, get an "update layout" event before it can finish up whatever housekeeping it needs to do. Then, perhaps, these properties will be set.
But this is all speculation on my part.
11-08-2009 11:56 AM |
Actually I am calling the update layout from the code behind after adding to the collection.
Min-Hong...
Contributor
3619 points
412 Posts
11-11-2009 9:42 PM |
Hi,
It is traverse when you are using hierachy datatemplate. But the treeview control will check if the type is fit. I mean if it is the same type. If not it will return a false value indicates it has no items.
For example
TreeView.ItemsSource = ObservableColleciton<MyItem>;
And the MyItem has a member property ObservableCollection<MyItem>, in this case the hierachy data template will be traversed.
If the member collection's object type is not MyItem then it will not. You can download the source code form CodeFx and check if that is the case.
Best Regards
11-15-2009 4:21 PM |
Min,
In this case, the ItemsSource is a ObservableCollection<xxx.productcategory> where xxx.productcategory is derived from ObservableCollection<xxx.productcategory> ... i.e.
public System.Collections.ObjectModel.ObservableCollection<productcategory> ProductCategories = new System.Collections.ObjectModel.ObservableCollection<productcategory>();public class productcategory : ObservableCollection<productcategory>{ private MoneyPinchers.Web.Category _category; private string _name; private Int64 _UID;}
I add sub-categories to the collection by selecting a category in the collection and using the inherited xxx.Add method, i.e. CurrentCategory.Add(newcategory), so I from what I understand the overall collection should be traversable as they appear as members of the Items attribute of the parent category.
Marc