locked
Data binding - ObservableCollection containing INotifyPropertyChanged containing ObservableCollection containing INotifyPropertyChanged

    Question

  • Hello,

    I'm having trouble getting my data binding in my XAML to work and I think it has something to do with my nested data structures. I have something like this:

    class MyObCollection : ObservableCollection<MySpecialINotif>
    {
       // Some not pertinent stuff
    }
    
    class MySpecialINotif : INotifyPropertyChanged
    {
       public ObservableCollection<MyNextSpecialINotif> Items;
    }
    
    class MyNextSpecialINotif : INotifyPropertyChanged
    {
       // Things I want to bind to
    }

    I can see that the first MyObCollectionis has the CollectionChanged event latched onto (non-null). I see that each MySpecialINotif has it's PropertyChanged event latched onto (non-null). I see that the 'Items' member CollectionChanged event is latched onto (non-null). However, my MyNextSpecialINotif object doesn't have it's PropertyCHanged event latched onto it.

    I know the inital binding works because I see the inital values correctly. It's just when I start updating the MyNextSpecialINotif objects I don't see any updates in my 'OneWay' binding.

    EDIT #1

    It turns out I was barking up the wrong tree with focusing on the nested structure. After some experimentation I think it's  because of how I defined XAML binding.

    I first had this:

    <ListView  ItemsSource="{Binding MiniRecentRejects, Mode = OneWay}">              
       <ListView.ItemTemplate>
          <DataTemplate>
             <GridView ItemsSource="{Binding Items, Mode=OneWay}">
                <GridView.ItemTemplate>
                   <DataTemplate>
                      <Rectangle Fill="{Binding Converter={StaticResource DataproviderResultToCellFillConverter}, Mode=OneWay}" Width="5" Height="5"  />
                   </DataTemplate>                                              
                </GridView.ItemTemplate>
             </GridView>
          </DataTemplate>
       </ListView.ItemTemplate>
    </ListView>
    


    Notice the Rectangle Binding, I'm trying to pass the whole item to the converter. But this didn't work and it's PropertyChanged event wasn't being listened to. So, no feedback when the object was changed.

    I then created a hack where I added a 'Self' data member to my class like this:

    class MyNextSpecialINotif : INotifyPropertyChanged
    {
            public MyNextSpecialINotif Self
            {
                get { return this; }
            }
    }

    I then instead of having a blank binding, I reference the new 'Self' in the XAML like this:

    <Rectangle Fill="{Binding Self, Converter={StaticResource DataproviderResultToCellFillConverter}, Mode=OneWay}" Width="5" Height="5"  />

    And this worked! 

    So the question is, How do I do this the right way with having to create the hack 'Self' data member?

    Joe

    Tuesday, January 06, 2015 12:11 AM

Answers

  • Hi Jfradley,

    You’ve not posted details about the convert class and the MiniRecentRejects object, so I may not clearly understand it. Per my understanding, the binding operation need Path at least to find something in object to bind. In this case, the Rectangle XAML need find one property from MyNextSpecialINotif class to bind to it. So the code snippet may look like the following. You cannot skip the Path property.

    Fill="{Binding Path=One property in MyNextSpecialINotif class, Converter={StaticResource DataproviderResultToCellFillConverter}, Mode=OneWay}"

    If this doesn’t help, please post more code snippets.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by jfradley Monday, January 12, 2015 3:37 PM
    Thursday, January 08, 2015 7:55 AM
    Moderator

All replies

  • Hi Jfradley,

    Can you explain why you need to use nested data structures? Can it be possible to use ObservalbleCollection<MyNextSpecialINotif> to instead directly? I think it should work.

    Please post a repro project to show the steps on how to reproduce your problem? I cannot guess how to use the three classes. Use your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, January 07, 2015 3:36 AM
    Moderator
  • Hi Herro,

    I'm trying to preserve the data structure as it's used elsewhere. I'd rather not have to create a new intermediate one for this. However, with that being said I think the concern over the nested structure of it was a 'red herring', I think my problem is in how I define the Binding in the XAML. Please see my updated post. Thank you for you response.

    Joe

    Wednesday, January 07, 2015 2:36 PM
  • Hi Jfradley,

    You’ve not posted details about the convert class and the MiniRecentRejects object, so I may not clearly understand it. Per my understanding, the binding operation need Path at least to find something in object to bind. In this case, the Rectangle XAML need find one property from MyNextSpecialINotif class to bind to it. So the code snippet may look like the following. You cannot skip the Path property.

    Fill="{Binding Path=One property in MyNextSpecialINotif class, Converter={StaticResource DataproviderResultToCellFillConverter}, Mode=OneWay}"

    If this doesn’t help, please post more code snippets.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Marked as answer by jfradley Monday, January 12, 2015 3:37 PM
    Thursday, January 08, 2015 7:55 AM
    Moderator