Keeping Track of 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 textbox,combobox,checkboxes, or radio buttons to allow the user to answer the question after they build some sort of questionnaire. My question is what is the best way to keep track of the answers across all the different controls after the controls are created and the questionnaire is created on the fly. Right now, i'm looping through all the containers and getting the values based on the controlType. Which this works fine but I'm wondering if databinding or something else would somehow provide me a better solution.
My hiccup with databinding is that i don't have the struct开发者_如何学Goure of the expected answers or questions until after everything is created so it can be different everytime. I know this is a little vague but i'd really appreciate any help anyone can provide. thanks.
Id create classes that represents each type of question (ie one that would require a testbox answer, one for combobox, etc). Also create a datatemplateselector to select which template you want and create a resource in the 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;
}
}
Instantiation Code:
public MainWindow()
{
InitializeComponent();
ObservableCollection<QuestionType> Questions = new ObservableCollection<QuestionType>()
{
new TextBoxQuestion() { Question = "What's 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>
精彩评论