Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:07:18+00:00 2026-05-23T03:07:18+00:00

I have a WPF application that allows users to create questions of all different

  • 0

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 structure 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:07:19+00:00Added an answer on May 23, 2026 at 3:07 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have a WPF application built upon SQLite database and user/application Configuration settings.
I have a wpf c# application, that loads tasks to a treeView from a
My application has a custom window design, which means that every window has the
Hi I am creating my first WPF application that uses Ribbons. The problem I
I have a WPF application, i created a new class named Agent . On
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have a snippet to create a 'Like' button for our news site: <iframe
I have a login.jsp page which contains a login form. Once logged in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.