views:

31

answers:

2

I'm having a tough time solving this simple issue :

I am using a treeview with HierarichalDataTemplate to show an hierarchy of two entities : Organization and Unit. The Organization has a many to many relation with itself as it could have sub organizations. And each Organization has one to many relation with Unit, as an organization could contain several units.

I am successfully using multibindings with observable collection to populate the tree. The converter is called in initialization, but i cant get it to be called again. So when i add any units or organizations, the list is not updated:

  <HierarchicalDataTemplate.ItemsSource>
      <MultiBinding Converter="{StaticResource TreeMultiValueConverter}">
          <Binding Path="ChildOrgs"/>
          <Binding Path="Units" />
      </MultiBinding>
  </HierarchicalDataTemplate.ItemsSource>

Please help... Thanks

+1  A: 

I think what's happening here is when you pass your collections to the converter (i.e. "TreeMultiValueConverter") you are merging the two into one collection, say a ObservableCollection and returning it as the source.

This is precisely why when you add a new item to your unit or organization collection, the collection changed event is not propagated to the ItemsSource of the HierarchicalDataTemplate.

Solution:

Return a list collection view in you converter and update the view on collection changed.

public object Convert(object[] values, Type targetType, object parameter, 
      System.Globalization.CultureInfo culture)
{
    if(values == null || values.Length != 2) return null;

    var combinedList = new List<object>();
    var listCollectionView= new ListCollectionView(combinedList);    

    var childOrgs = values[0] as ObservableCollection<Organization>;
    if(childOrgs != null)
    {
        combinedList.AddRange(childOrgs);
        childOrgs.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }
    var units = values[1] as ObservableCollection<Unit>;
    if(units != null)
    {
        combinedList.AddRange(units);
        units.CollectionChanged += (s,e) => listCollectionView.Refresh();
    }

    listCollectionView.Refresh();
    return listCollectionView;
}
Tri Q
Thanks. I have tried your solution but it seems that following line returns null as the values[0] is actually an EntityCollection and is runtime casted to observableCollection : var childOrgs = values[0] as ObservableCollection<Organization>; Any ideas?
OrPaz
It seems like you've bind directly to your Entity models? Try changing the cast from ObservableCollection`1 to EntityCollection.
Tri Q
We can't to that as we need both childOrgs and units to be observables so we can use the CollectionChanged event. EntityCollection does not contains this event.
OrPaz
Thanks for your reply. I have found another solution and posted it. +1 for you!
OrPaz
A: 

I have solved my problem using partial extending of my entity class, and adding a 'MixedChilds' collection which contains both Organization and Unit types together. When the HierarchialDataTemplate is bound to that, everything works.

OrPaz