0

I have this code on my xaml.

public partial class MainWindow : Window
    {
        List<question> questions = new List<question>();
        public MainWindow()
        {
            InitializeComponent();

            questions.Add(new question {number=1, content="1+1?", answer="2"});
            questions.Add(new question {number=2, content="1+2?", answer = "3"});
            this.DataContext = questions;
        }
    }

    public class question
    {
        public int number { get; set; }
        public string content { get; set; }
        public string answer { get; set; }
    }

The above i created a class for questions and inserted rows on it.

now on my wpf, how should i display those 2 questions that i have inserted. Just a simple databinding.

Text="{Binding number}" Text="{Binding content}"

Unfortunately, if i do the above twice to show the 2 questions i inserted it will only duplicate what is shown on the first one. To put it simply, if i had an array with 2 values inside it and i want to display both the values and just use echo or any printing code without looping it will only output the first value or the 2nd value if it acts to overwrite the first one.

Im new to C# WPF. What and how should i be doing to display both questions. in separate textblocks.

1.) 1+1?

2.) 1+2?

Additional question, For choices, ill use radio buttons. 3 of them. Should i add 3 more property on my question class for choice1 choice2 choice3 then use databind to put those value in my radio buttons? or should i just simply, put those values manually in those radio buttons.he

Additional question, I will put a button to submit the answers, Ill be making another wpf window and name it result. How can i pass the total number of questions and the questions that are answered correctly so i can show on the result page the score of the exam.

Ive taken electronic classroom exams myself, after every exam the result page not only shows the total score it also shows the questions and the choice answered by the user and the correct answer. How do i pass those questions that i inserted on my question class if i could pass those 2 questions i inserted should i then just add 3 more property to my class for choices, like choice1 choice2 choice3 then

    questions.Add(new question {number=1, content="1+1?", answer="2", choice1="3", choice2="1", choice3="2"});
    questions.Add(new question {number=2, content="1+2?", answer = "3", choice1="3", choice2="1", choice3="2"});

Then on my class, public string choice1 { get; set; } until choice3. Since its a string i might need to use convert.toint16()

user3205047
  • 199
  • 1
  • 3
  • 14

1 Answers1

0

You should use a Listview to display all your questions (that I supposed to be more than two). Each row of the ListView is a data template with a TextBlock and 3 Radio Buttons. You should insert the 3 choices in the question class and bind them to the radio group.

Something like this:

<ListView ItemsSource="{Binding Path=questions}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text={Binding Path=content}"/>
                <RadioButton GroupName="{Binding Path=Number}" Content="{Binding Path=Choice1"/>
                <RadioButton GroupName="{Binding Path=Number}" Content="{Binding Path=Choice2"/>
                <RadioButton GroupName="{Binding Path=Number}" Content="{Binding Path=Choice3"/>
            </StackPanel>
        </DataTemplate>
    <ListView.ItemTemplate>   
</ListView>
rPulvi
  • 946
  • 8
  • 33