我正在为我的同学做一个WPF桌面应用程序,因此他们可以练习德语的过去时。目前,我能够从txt文件中随机生成8个问题,并使用正确的答案填充组合框。我当前的目标是在用户单击评估按钮后将组合框的前景色更改为绿色/红色。如果用户选择正确的答案,则前景色变为绿色;如果用户选择错误的答案,则前景色变为红色。
在测试类中,我为我的答案,问题和正确答案初始化ObservableCollection。然后逐行读取txt文件,并相应地填充ObservableCollections。在我的MainWindow中,将DataConext设置为我的Test Class实例,并将组合框绑定到具有适当答案的ObservableCollection。我可以在后台代码中实现我想要的结果(如下面的代码所示)。但是,例如,如果我的测试模式比一种测试模式多,那么我将不得不在代码后面的每个窗口中评估和更改颜色,这似乎是不切实际的。
因此,我的最终目标是以某种方式为所有带有x:Key =“ ExamComboBoxStyle”的组合框创建触发器(例如,在App.Resources中),如果用户点击评估按钮并根据所选答案更改前景色。有没有在XAML中执行此操作的聪明方法?
我有一些相关的stackoverflow线程,但是不幸的是,由于我对XAML和WPF还是很陌生,所以我无法根据自己的需要来应用它们。
WPF Textblock text does not change dynamically on combobox selected item
ComboBox SelectedValue or SelectedItem Binding WPF C#
测试类:
class Test : INotifyPropertyChanged {
List<string> questionsAndAnswers = new List<string> ();
List<int> questionsOrder = new List<int> ();
List<int> generatedNumbers = new List<int> ();
private ObservableCollection<string> _questions;
public ObservableCollection<string> questions {
get { return _questions; }
set {
_questions = value;
}
}
private ObservableCollection<ObservableCollection<string>> _listOfQuestionsList;
public ObservableCollection<ObservableCollection<string>> listOfQuestionsList {
get { return _listOfQuestionsList; }
set {
_listOfQuestionsList = value;
}
}
private ObservableCollection<string> _answersForCombobox;
public ObservableCollection<string> answersForCombobox {
get { return _answersForCombobox; }
set {
_answersForCombobox = value;
}
}
private ObservableCollection<string> _correctAnswers;
public ObservableCollection<string> correctAnswers {
get { return _correctAnswers; }
set {
_correctAnswers = value;
}
}
public Test() {
answersForCombobox = new ObservableCollection<string> ();
questions = new ObservableCollection<string> ();
listOfQuestionsList = new ObservableCollection<ObservableCollection<string>> ();
correctAnswers = new ObservableCollection<string> ();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property) {
if (PropertyChanged != null) {
PropertyChanged (this, new PropertyChangedEventArgs (property));
}
}
MainWindow:
public partial class MainWindow : Window
{
Test newTest = new Test();
public MainWindow()
{
InitializeComponent();
DataContext = newTest;
newTest.GetVerbsFromFile();
newTest.AddingQuestionsAndAnswers();
}
private void evaluateButton_Click(object sender, RoutedEventArgs e)
{
if (AnswerCombobox1.SelectedValue == newTest.correctAnswers[0]) {
AnswerCombobox1.Foreground = Brushes.LimeGreen;
} else {
AnswerCombobox1.Foreground = Brushes.Red;
}
if (AnswerCombobox2.SelectedValue == newTest.correctAnswers[1]) {
AnswerCombobox2.Foreground = Brushes.LimeGreen;
} else {
AnswerCombobox2.Foreground = Brushes.Red;
}
if (AnswerCombobox3.SelectedValue == newTest.correctAnswers[2]) {
AnswerCombobox3.Foreground = Brushes.LimeGreen;
} else {
AnswerCombobox3.Foreground = Brushes.Red;
.......
if (AnswerCombobox8.SelectedValue == newTest.correctAnswers[7]) {
AnswerCombobox8.Foreground = Brushes.LimeGreen;
} else {
AnswerCombobox8.Foreground = Brushes.Red;
}
}
}
}
XAML:
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock
Style="{StaticResource AnswersTextBlockStyle}"
Name="Question1"
Text="{Binding questions[0]}"
Grid.Column="0"
Grid.Row="1" />
<ComboBox
Style="{StaticResource ExamComboBoxStyle}"
Name="AnswerCombobox1"
ToolTip="{Binding correctAnswers[0]}"
ItemsSource="{Binding listOfQuestionsList[0]}"
Grid.Column="1"
Grid.Row="1"/>
</Grid>
........
<Grid Grid.Row="9">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button
Name="graphsButton"
Content="graphs"
Click="graphsButton_Click" Grid.Column="1" Margin="598.2,0,-759.4,-0.2" />
<Button
Name="evaluateButton"
Content="evaluate"
Click="evaluateButton_Click" Grid.Column="2" Margin="395.4,0,-556.6,0" />
</Grid>
只是为了使我的期望结果看起来更加清楚: