How to get listview header context in item

DaWaDaWa Member ✭✭

Hi,
I am using a grouped ListView and want to show information of the group header displayed in the item.

Here is my xaml code:

<ListView ItemsSource="{Binding ParentList}" IsGroupingEnabled="true">
  <ListView.GroupHeaderTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout>
          <Label Text="{Binding ParentTitle}"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.GroupHeaderTemplate>

  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <StackLayout>
          <Label Text="{Binding ParentTitle}"/> <!-- Here I want to get the GroupHeader Context -->
          <Label Text="{Binding ChildTitle}"/>
        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Here is my Parent class:

public class Parent : ObservableCollection<Child>
{
  public ObservableCollection<Child> ChildList => this;
  public String ParentTitle { get; set; }
}

Here is my Child class:

public class Child
  public String ChildTitle { get; set; }
}

I already tried setting a x:Name on the GroupHeader but it is not unique so the item can not find it.

Can anyone please help?

Answers

  • DaWaDaWa Member ✭✭

    @AnubhavRanjan Thanks, I checked your link, but I can not see how it will help with my problem.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Assuming you have multiple parent objects with multiple children for each parent:

    Instead of trying to do it in your XAML, how about adding the ParentTitle as a property to your Child class, or a reference to the Parent object in the Child.

    The former approach will then allow you to just bind to the ParentTitle property in the ItemTemplate.

    I anticipate that the latter can/will introduce a circular reference which can impact garbage collection, if you don't clear down your parent/child collections.

  • DaWaDaWa Member ✭✭

    @JamesLavery thanks for your help, but I wanted prevent a circular reference.

    I simplified my real problem to show what I need. In my project I need want to disable my child when my parent is disabled and I also need the Id of my parent in the itemtemplate.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    So you've added the Parent Id to the child item?

  • DaWaDaWa Member ✭✭

    No, not yet. I hope that I do not need to do that.

  • DaWaDaWa Member ✭✭

    Since I did not get an other solution I was forced to add the necessary fields to the ChildObject.

Sign In or Register to comment.