Skip to main content

Microsoft Silverlight

Answered Question Setting the TreeView "HasItems" Property when using a ObservableCollectionRSS Feed

(0)

alaskanrogue
alaskanr...

Member

Member

19 points

67 Posts

Setting the TreeView "HasItems" Property when using a ObservableCollection

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...

 

 

 

 

 

 

 

 

Marc W. George
One Thought, One Message, "Live!"

K2P2
K2P2

Participant

Participant

1028 points

337 Posts

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

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.

alaskanrogue
alaskanr...

Member

Member

19 points

67 Posts

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

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.

Marc W. George
One Thought, One Message, "Live!"

K2P2
K2P2

Participant

Participant

1028 points

337 Posts

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

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.

alaskanrogue
alaskanr...

Member

Member

19 points

67 Posts

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

Actually I am calling the update layout from the code behind after adding to the collection.

Marc W. George
One Thought, One Message, "Live!"

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3376 points

377 Posts

Answered Question

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

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

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

alaskanrogue
alaskanr...

Member

Member

19 points

67 Posts

Re: Setting the TreeView "HasItems" Property when using a ObservableCollection

Min,

Thanks for replying. 

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

 

Marc W. George
One Thought, One Message, "Live!"
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities