0

I have typical case of nested collections. I have to make a questionary form. I have ObservableCollection of Questions that are part of the questionary. For each question, according to the question type, I have to display answers. Thus need to create template for nested binding. How can I do that?

Objects:

Question
     Question
     Answers

Here is part of my code:

    <ListView ItemsSource="{Binding Path=ocQuestions, Mode=TwoWay}"                         x:Name="lvQuestions" HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel DockPanel.Dock="Top" Background="#FF3C3C3C">
                    <StackPanel>
                        <StackPanel.Style>
                            <Style TargetType="StackPanel">
                                <Setter Property="Visibility" Value="Collapsed"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Type}" Value="B">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Style>
                        <ListView DataContext="{Binding SelectedItem, ElementName=lvQuestions}" ItemsSource="{Binding Answers}">
                            <Label Style="{StaticResource CampaignAnswer}" >
                                <CheckBox Content="{Binding Answer}"/>
                            </Label>
                        </ListView>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>
2

1 Answer 1

2

don't change DataContext of nested ListView

create ItemTemplate for nested ListView

make sure DataTrigger Binding="{Binding Type}" is correct bound and shows StackPanel properly (try without StackPanel style until you ensure that answers are displayed)

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Background="#FF3C3C3C"
                    DockPanel.Dock="Top">
            <StackPanel>
                <StackPanel.Style>
                    <Style TargetType="StackPanel">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Type}"
                                         Value="B">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>

                <Label Content="{Binding QuestionText}" />
                <ListView ItemsSource="{Binding Answers}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.