Binding context to nested object

I am trying to bind a nested object (ClassObject) for example.

{
Name = Mike
Location= USA
Classes
{
class= math, time=1pm,
class= english, time=2pm
}

}

In my cs code, I bind this to my bindingContext
BindingContext = ClassObject

However, I unable to access the nested object(Classes) on my xaml page when I bind it. On my xaml page, for the itemsource, I have ItemsSource="{Binding .}" then for the loop, I have classes.class, classes.time etc. However, this shows up blank. I am kind of new to this and I am not sure what I am doing wrong. Any help is greatly appreciated.

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    You should bind ItemSource to Classes
  • WeezyWeezy USMember ✭✭
    I am able to access the other properties of that class (name, location). I am just looking for a way to access the nested inner information to where I can display in a loop
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Use a listview for Classes properties
  • WeezyWeezy USMember ✭✭
    edited January 2018

    I am doing that. But my question is how to I access the inner information. What I have on my xaml is the following

        <ListView ItemsSource="{Binding .}"> <- here I am attaching the context to the ItemsSource
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout>
                                <Label Text="{Classes.class}"></Label> <- this is where my problem is
                                <Label Text="{Classes.time}"></Label>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    But this doesnt show any information. However I can do

        <Label Text ="{Binding Name}" />
        <Label Text ="{Binding Location}" />
    

    And that works. So it looks like I have no problem access the first level of the binding context. It is the second level I am not able to access. I am new to this so sorry if I am not wording it right. Thanks

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    This is broken

    <Label Text="{Classes.class}"></Label> <- this is where my problem is

    versus

    this works

    <Label Text ="{Binding Name}" />

    Notice how your broken binding doesn't say it is binding?

    try <Label Text="{Binding Classes.class}"/>

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Try to

    "{Binding Classes}"
    

    and

                           <Label Text="{class}"></Label> <- this is where my problem is
                            <Label Text="{time}"></Label>
    

    And post your full "model"

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @AlessandroCaliaro said:
    Try to

    "{Binding Classes}"

    and

                           <Label Text="{class}"></Label> <- this is where my problem is
                            <Label Text="{time}"></Label>
    

    And post your full "model"

    There is no need to see the model. Look at the markup again. He's not stating that there is any binding.
    Text="{Classes.class}" - wrong
    Text="{Binding Classes.class}" - right

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @ClintStLaurent you are right.

  • WeezyWeezy USMember ✭✭

    Thank you for your response. As per your suggestion,

    I have tried

    <StackLayout>
        <ListView ItemsSource="{Binding .}"> 
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout>
                                <Label Text="{Binding Classes.class}"></Label>
                                <Label Text="{Binding Classes.time}"></Label>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    I also tried

    <StackLayout>
        <ListView ItemsSource="{Binding Classes}"> 
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout>
                                <Label Text="{Binding Classes.class}"></Label>
                                <Label Text="{Binding Classes.time}"></Label>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    None of these worked.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    If you use the first you should bind to Classes.class
    If you use the second you should bind to class

    Post your model

  • WeezyWeezy USMember ✭✭
    public class ClassesModel
    {
        public string Name { get; set; }
        public string Location { get; set; }
        public List<Classes> class { get; set; } <- this has the class & time properties
    }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Tell ya what... Go run through my tutorials. Actually build the sample application - don't just scan it for an answer.
    The sample app has nested binded properties. Its a little example app with a collection of Pet objects with three dogs to start. You'll see it display the Pet.Name, Pet.Color and so on.
    http://www.RedPillXamarin.com

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited January 2018

    @Weezy said:
    public class ClassesModel
    {
    public string Name { get; set; }
    public string Location { get; set; }
    public List class { get; set; } <- this has the class & time properties
    }

    Changes to the List are not going to magically bubble up. Its not an ObservableCollection. Additions or changes to the elements of the collection do not constitute a change to the the actual collection as an object. The instance of class always remains the same instance and therefore there are not any events telling the UI of a change it should display.

    So I think the problem is not that the binding is broken, but rather you're expecting to see the UI update when the collection changes and that's just not going to happen. Its not that the binding is broken but rather your expectations and understanding of properties/collections/element-in-collections/notification-events is wrong.

    It also looks like not of your properties are implementing INotifyPropertyChanged - which is concerning.

  • aquabayaquabay AUMember ✭✭
    edited September 13

    .

Sign In or Register to comment.