I’m working in a survey form. The survey is created from an object with the following structure:
Survey –has–> Sections –has–> Questions –has–> QuestionOptions
Each QuestionOption has:
QuestionID (relate to the Question)
OptionText (What will be displayed in ComboBox (DisplayMemeber binding))
OptionValue (The score of the option, usually 1-5 (SelectedValuePath binding))
Results for a Survey are stored in the folowwing structure:
Result –has–> Answers
Each Answer has:
QuestionID (to link answer to survey option in database)
Score (This comes from the value selecter by user in the ComboBox)
Saving works fine, but if I need to Edit a Survey, I need to bring back the results chosen so far. The object “Result” is populated perfectly, so I have all the data available.
The Problem is in GUI:
How should I initialize or set each ComboBox created from DataTemplate with the corresponding value of the stored survey? Take in consideration the ComboBox is generated from object “Survey” and the answers are in object “Result”
The XAML code used for ComboBox is:
<DataTemplate x:Key="QuestonTemplate">
<StackPanel Margin="10,2,10,2" Orientation="Vertical">
<TextBlock HorizontalAlignment="Left" Text="{Binding Path=QuestionText}" TextWrapping="Wrap" Height="Auto" Margin="5" FontSize="14" />
<ComboBox x:Name="Options" Grid.Row="1" HorizontalAlignment="Left" Width="400" Margin="10,0,10,0"
Style="{StaticResource FlatComboBoxPaleYellow}"
ItemsSource="{Binding Path=QuestionOptions}"
SelectedValuePath="OptionValue"
DisplayMemberPath="OptionText"
SelectionChanged="Answer_SelectionChanged" />
</StackPanel>
</DataTemplate>
I’m open to XAML code or C# (even better! I’m old school) or any suggestion.
Thanks!
I would recommend that your Question object holds an additional property: SelectedOption
This would be data bound to the SelectedItem of your combo box:
Then in your Survey object, for each Question in your Questions collection, find any of the Answers in the Result object that correspond to that question, lookup the corresponding QuestionOption on that question and assign it to the SelectedOption property of that Question object.