Track values ​​with dynamically created controls

I have a WPF application that allows users to create questions of all different types, which, depending on the type, will use a text box, combo box, checkboxes or radio buttons to allow the user to answer a question after creating any questionnaire. My question is The best way to track responses to all the various controls is after creating the controls, and the questionnaire is created on the fly. Right now, I iterate over all containers and get the values ​​based on controlType. That this works great, but I wonder if data binding or something else could somehow provide me with a better solution.

My data binding hiccup is that I don't have the structure of the expected answers or questions until everything is created so that it can be different each time. I know this is a bit vague, but I would really appreciate any help anyone can provide. thanks.

+4
source share
1 answer

The identifier creates classes that represent each type of question (i.e. the one for which a test window answer is required, one for combobox, etc.). Also create a data item to select the template you want and create a resource in xaml for this selector.

Classes:

public abstract class QuestionType { public string Question { get; set; } } public class TextBoxQuestion : QuestionType { public string Answer { get; set; } } public class CheckBoxQuestion : QuestionType { public bool Answer { get; set; } } public class ComboBoxQuestion : QuestionType { public List<string> Values { get; set; } public string Answer { get; set; } } public class QuestionTemplateSelector : DataTemplateSelector { public DataTemplate Combo { get; set; } public DataTemplate Text { get; set; } public DataTemplate Check { get; set; } public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) { if (item is TextBoxQuestion) return Text; if (item is ComboBoxQuestion) return Combo; if (item is CheckBoxQuestion) return Check; return null; } } 

Activation code:

  public MainWindow() { InitializeComponent(); ObservableCollection<QuestionType> Questions = new ObservableCollection<QuestionType>() { new TextBoxQuestion() { Question = "What your favorite color?" }, new CheckBoxQuestion() { Question = "Are you allergic to peanuts?" }, new ComboBoxQuestion() { Question = "How many fingers am I holding up?", Values = new List<string>() { "1", "2", "3", "4", "6" }} }; QuestionList.ItemsSource = Questions; } 

XAML:

 <Grid> <Grid.Resources> <local:QuestionTemplateSelector x:Key="questionSelector"> <local:QuestionTemplateSelector.Check> <DataTemplate DataType="local:CheckBoxQuestion"> <StackPanel> <TextBlock Text="{Binding Question}"/> <CheckBox IsChecked="{Binding Answer}"/> </StackPanel> </DataTemplate> </local:QuestionTemplateSelector.Check> <local:QuestionTemplateSelector.Text> <DataTemplate DataType="local:TextBoxQuestion"> <StackPanel> <TextBlock Text="{Binding Question}"/> <TextBox Text="{Binding Answer}"/> </StackPanel> </DataTemplate> </local:QuestionTemplateSelector.Text> <local:QuestionTemplateSelector.Combo> <DataTemplate DataType="local:ComboBoxQuestion"> <StackPanel> <TextBlock Text="{Binding Question}"/> <ComboBox SelectedValue="{Binding Answer}" ItemsSource="{Binding Values}"/> </StackPanel> </DataTemplate> </local:QuestionTemplateSelector.Combo> </local:QuestionTemplateSelector> </Grid.Resources> <ListBox Name="QuestionList" ItemTemplateSelector="{StaticResource questionSelector}"/> </Grid> 
+4
source

All Articles