charmosz
charmosz

Reputation: 133

How to bind the index of an element in a listview in the ItemsSource of a second listview inside the first one?

In my QuestionsPage I have a ListView (AnswersListView) inside another listview (QuestionsListView). How can I make a reference of the index of the question (which is in QuestionsListView) in the ItemsSource of the AnswersListView ? To try to make a it clearer, here is a simplified code... I would like to replace the ??? by the index of the question. Hope it is understandable.

In my XAML :

<ListView x:Name="QuestionsListView"
    ItemsSource="{Binding Challenge.questions}"
    <ListView.ItemTemplate>
        <DataTemplate>

            <ListView x:Name="AnswersListView"
                ItemsSource="{Binding Challenge.questions[???].answers, Source={x:Reference QuestionsPage}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <local:AnswerButton Text="{Binding isCorrect}" Command="{Binding BindingContext.CheckAnswerCommand, Source={x:Reference QuestionsPage}" CommandParameter="{Binding isCorrect}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And my models (to understand how one is related to the other):

public class ChallengeModel {   
    public int challengeId { get; set; }    
    public string challengeName { get; set; }   
    public ObservableCollection<Question> questions { get; set; }

    public ChallengeModel() { }
}

public class Question {     
    public string questionText { get; set; }    
    public ObservableCollection<Answer> answers { get; set; }

    public Question() { } 
}

public class Answer {   
    public bool isCorrect { get; set; }     
    public string answerText { get; set; }

    public Answer() { }
}

Upvotes: 2

Views: 1663

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39092

You don't have to reference it from the original DataContext. Using ItemsSource you are essentially setting the Challenge.question property as the data source for the list. Now when you define a ListView.ItemTemplate, you are actually in the context of one single question from the list. so every {Binding} inside the DataTemplate is relative to the question itself (unless you specify otherwise).

This means, you can just use {Binding answers} to bind the inner list to the list of available answers:

<ListView x:Name="QuestionsListView"
    ItemsSource="{Binding Challenge.questions}"
    <ListView.ItemTemplate>
        <DataTemplate>    
            <ListView x:Name="AnswersListView"
                ItemsSource="{Binding answers}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <local:AnswerButton Text="{Binding isCorrect}" Command="{Binding BindingContext.CheckAnswerCommand, Source={x:Reference QuestionsPage}" CommandParameter="{Binding isCorrect}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 2

Related Questions