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>
ASh
  • 34,632
  • 9
  • 60
  • 82
Bojan Kraut
  • 187
  • 1
  • 3
  • 8
  • Do not do that in one usercontrol. Create UI and viewmodel for subItems and continute all the way up. https://wpftutorial.net/DataTemplates.html is something to read... – Divisadero Feb 21 '18 at 12:01
  • Possible duplicate of [TreeView, HierarchicalDataTemplate and recursive Data](https://stackoverflow.com/questions/15240326/treeview-hierarchicaldatatemplate-and-recursive-data) – Rekshino Feb 21 '18 at 12:23

1 Answers1

1

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>
ASh
  • 34,632
  • 9
  • 60
  • 82