我是新的wpf C#和我试图通过创建我的第一个项目来学习,我得到了问题在堆栈面板,当我点击一个问题,它显示了我的问题和4个答案作为单选按钮和我试图保存当前的单选按钮被点击的答案,当我点击另一个问题,所有的单选按钮被清除,所以在我的代码中,当我点击另一个问题时,所有的单选按钮都被清除,但是当我回到已经回答的问题时,所有的单选按钮都被清除,而没有保存,这是我的C#代码:
using final_project.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
namespace final_project.View
{
/// <summary>
/// Interaction logic for StudentExamWindow.xaml
/// </summary>
public partial class StudentExamWindow : Window
{
private int CurrentQuestion;
private int Grade = 0;
List<int> qgrades = Enumerable.Repeat(0, 10).ToList();
Exam exam1 = new Exam();
List<Answer> answer1 = new List<Answer>();
Question que = new Question();
List<RadioButton> radioBoxes = new List<RadioButton>();
List<Button> buttons = new List<Button>();
DispatcherTimer dispatcherTimer;
TimeSpan time;
public StudentExamWindow()
{
InitializeComponent();
time = TimeSpan.FromHours(1);
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Start();
exam1.Id = "1234";
exam1.Name = "math";
exam1.ExamDate = DateTime.Now;
exam1.Questions = new List<Question>();
answer1.Add(new Answer { Id = "1", Content = "12" });
answer1.Add(new Answer { Id = "2", Content = "16" });
answer1.Add(new Answer { Id = "3", Content = "17" });
answer1.Add(new Answer { Id = "4", Content = "18" });
answer1.Add(new Answer { Id = "1", Content = "50" });
answer1.Add(new Answer { Id = "2", Content = "60" });
answer1.Add(new Answer { Id = "3", Content = "70" });
answer1.Add(new Answer { Id = "4", Content = "80" });
answer1.Add(new Answer { Id = "1", Content = "50" });
answer1.Add(new Answer { Id = "2", Content = "60" });
answer1.Add(new Answer { Id = "3", Content = "70" });
answer1.Add(new Answer { Id = "4", Content = "80" });
addquestions(exam1.Questions);
}
//Add question
private void addquestions(List<Question> questoins)
{
Question q = new Question();
Question q2 = new Question();
Question q3 = new Question();
q.Id = "Q1";
q.Name = "what 4 * 4 ? ";
q.Grade = "10";
q.Answers = answer1;
q.CorrectAnswerId = "second";
questoins.Add(q);
q2.Id = "Q2";
q2.Name = "what 4*79 ? ";
q2.Grade = "10";
q2.Answers = answer1;
q2.CorrectAnswerId = "third";
questoins.Add(q2);
q3.Id = "Q3";
q3.Name = "what 8*7 ? ";
q3.Grade = "10";
q3.Answers = answer1;
q3.CorrectAnswerId = "forth";
questoins.Add(q3);
AddQuestionPannel(questoins);
for(int i=0; i < buttons.Count; i++)
{
buttons[i].Name = "Q" + (i + 1).ToString();
}
}
//add Question pannel
private void AddQuestionPannel(List<Question> q)
{
foreach(Question rs in q)
{
int i = 0;
Button QB= new Button();
QB.Content = rs.Name;
QB.Background = Brushes.White;
QuestionPannel.Children.Add(QB);
QB.Click += new RoutedEventHandler(ShowQuestion_Click);
i++;
buttons.Add(QB);
}
}
//show the Question on the front of the page
private void ShowQuestion(string name,int index)
{
int i = index - 1;
int imul = i * 4;
radioBoxes.Clear();
Txtblock.Text = name;
Exercise.Text = buttons[i].Content.ToString();
first.Content = answer1[imul].Content;
second.Content = answer1[imul+1].Content;
third.Content = answer1[imul+2].Content;
forth.Content = answer1[imul+3].Content;
radioBoxes.Add(first); radioBoxes.Add(second);
radioBoxes.Add(third); radioBoxes.Add(forth);
}
private void AddGrade()
{
TextBlock gradeBlock= new TextBlock();
gradeBlock.Text = Grade.ToString();
CheckboxList.Children.Add(gradeBlock);
}
private bool CheckAnswer(string c,string correctanswer)
{
if (c==correctanswer)
{
return true;
}
return false;
}
private bool Buttonwasclicked = false;
//show the question on the front of the page click
private void ShowQuestion_Click(object sender, RoutedEventArgs e)
{
if(!Buttonwasclicked)
{
foreach(RadioButton r in radioBoxes)
{
r.IsChecked = false;
Buttonwasclicked = true;
}
}
SaveAnswer.Content = "Save Answer";
int temp = 0;
Exercise.Visibility = Visibility.Visible;
first.Visibility= Visibility.Visible;
second.Visibility= Visibility.Visible;
third.Visibility= Visibility.Visible;
forth.Visibility= Visibility.Visible;
SaveAnswer.Visibility= Visibility.Visible;
string Btn_name = (sender as Button).Name.ToString();
while("Q" + temp.ToString() != Btn_name)
{
temp++;
}
CurrentQuestion = temp-1;
ShowQuestion(Btn_name,temp);
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Save_Click(object sender, RoutedEventArgs e)
{
AddToFinalGrade();
AddGrade();
}
//save the current question's answer
private void SaveAnswer_Click(object sender, RoutedEventArgs e)
{
SaveAnswer.Content = "Saved";
int grade = 0;
for (int i = 0; i < radioBoxes.Count; i++)
{
if (radioBoxes[i].IsChecked == true && CheckAnswer(radioBoxes[i].Name, exam1.Questions[CurrentQuestion].CorrectAnswerId))
{
grade = 10;
break;
}
else
{
grade = 0;
}
}
AddToGredeList(grade, CurrentQuestion);
}
private void AddToGredeList(int g, int i)
{
qgrades[i] = g;
}
private void AddToFinalGrade()
{
Grade = 0;
for (int i = 0; i < qgrades.Count; i++)
{
Grade += qgrades[i];
}
}
private void first_Checked(object sender, RoutedEventArgs e)
{
}
private void second_Checked(object sender, RoutedEventArgs e)
{
}
private void third_Checked(object sender, RoutedEventArgs e)
{
}
private void forth_Checked(object sender, RoutedEventArgs e)
{
}
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
if (time == TimeSpan.Zero) dispatcherTimer.Stop();
else
{
time = time.Add(TimeSpan.FromSeconds(-1));
MyTime.Text = time.ToString("c");
}
}
}
}
下面是我的XAMl代码:
<Window x:Class="final_project.View.StudentExamWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:final_project.View"
mc:Ignorable="d"
Title="StudentExamWindow" Height="450" Width="800">
<Border Background="#eff2f7" CornerRadius="30">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!--Left Side-->
<Grid Grid.Column="0"/>
<Border BorderBrush="Black" BorderThickness="4">
<StackPanel Name="QuestionPannel">
</StackPanel>
</Border>
<!--Right Side-->
<Border CornerRadius="30" Grid.Column="1">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition />
</Grid.RowDefinitions>
<!-- Title -->
<Grid Grid.Row="0"
Width="550"
Background="#2e3137">
<StackPanel >
<DatePicker Height="25" Width="115"
HorizontalAlignment="Left"
Margin="10 10 10 0"
Name="datePicker1"
VerticalAlignment="Top"/>
</StackPanel>
<Grid Margin="150,0,0,0">
<Label Name="Examtitle" Content="Exam Name"
Foreground="White"
Margin="10" />
</Grid>
</Grid>
<!--Question Show-->
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Name="MyTime" Text="1:00:00" Margin="273,10,274,10" />
<TextBlock Name="StudentName" Text="Studentname" Margin="10,10,553,10" />
<Border BorderBrush="Black" BorderThickness="4">
<Grid Grid.Row="0" Margin="0,10,0,0">
</Grid>
</Border>
<Grid Grid.Row="1" Margin="0,5,0,0">
<TextBlock Text="Exercise NAME" Name="Exercise" Visibility="Hidden"
HorizontalAlignment="Center"
Width="350"
Margin="0,0,0,289" />
<Border BorderBrush="Black" BorderThickness="4" Margin="0,-16,0,0">
<StackPanel Name="CheckboxList" Width="350" Margin="69,23,0,37" HorizontalAlignment="Left" Cursor="">
<TextBlock Name="Txtblock" TextWrapping="Wrap" Text=""/>
<RadioButton Name="first" Content="" Visibility="Hidden" Checked="first_Checked"/>
<RadioButton Name="second" Content="" Visibility="Hidden" Checked="second_Checked"/>
<RadioButton Name="third" Content="" Visibility="Hidden" Checked="third_Checked"/>
<RadioButton Name="forth" Content="" Visibility="Hidden" Checked="forth_Checked"/>
<Button x:Name="SaveAnswer" Content="Save Answer"
Visibility="Hidden" Style="{StaticResource ResourceKey=RoundButtonTemplate}"
Width="108"
Height="33"
BorderBrush="Aqua"
Background="black" Click="SaveAnswer_Click"/>
</StackPanel>
</Border>
<Button Name="Save" Content="Save All" Visibility="Visible" HorizontalAlignment="Left" Margin="551,231,0,0" VerticalAlignment="Top" Height="31" Width="68" RenderTransformOrigin="0.859,1.023" Click="Save_Click">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="0.332"/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
</Button>
<Button Name="Exit" Visibility="Visible" Content="Exit" HorizontalAlignment="Left" Margin="551,271,0,0" VerticalAlignment="Top" Height="27" Width="72" Click="Exit_Click"/>
</Grid>
</Grid>
</Grid>
</Border>
</Grid>
</Border>
</Window>
我试图创建一个bool变量来检查哪个按钮作为一个问题被点击,所以我不清除单选按钮,但仍然没有工作,它清除了点击和不点击一次
1条答案
按热度按时间lstz6jyr1#
下面的示例使用数据绑定和数据模板实现了一个小的简单测验,以启用动态UI(例如,随着
RadioButton
元素的动态创建,选择的数量可能会有所不同)。由于数据绑定,视图的值会自动传输并存储在数据模型中。该示例还展示了如何使用组合正确地设计类及其关系。
希望这个例子能帮助您理解如何应用基本的WPF技术
Exam.cs
问题.cs
答案.cs
MainWindow.xaml.cs
主窗口.xaml