我正在创建一个WPF应用程序,允许用户进行测验然后提交他们的答案并在另一个页面/ xaml / window中检查结果。我的第一页显示了ListView中的问题列表和单个“提交”按钮。我的ListView中的问题是从我创建的名为“Question”的类中绑定的。它有7个属性,即:number,problem,choice1,choice2,choice3,choice4和correct_answer。 ListView中不显示属性“correct_answer”。如果用户提交他们的答案并查看结果,我希望能够在另一个包含在TreeView中的xaml中显示具有正确答案的相同问题列表。我的第二页显示了测验的分数和结果。喜欢检查正确答案的用户可以扩展TreeView。
QuizWindow.xaml
<ListView Grid.Row="2" Name="list_question" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="{Binding number}"></Label>
<Label Grid.Column="1" Content="{Binding problem}"></Label>
<RadioButton Grid.Row="1" Content="{Binding choice1}"></RadioButton>
<RadioButton Grid.Row="2" Content="{Binding choice2}"></RadioButton>
<RadioButton Grid.Row="1" Grid.Column="1" Content="{Binding choice3}"></RadioButton>
<RadioButton Grid.Row="2" Grid.Column="1" Content="{Binding choice4}"></RadioButton>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Grid.Row="3" Content="Submit" Name="btn_submit" Click="btn_submit_Click"></Button>
按钮点击事件以查看考试结果。 QuizWindow.xaml.cs
private void btn_submit_Click(object sender, RoutedEventArgs e)
{
ResultWindow resultWindow = new ResultWindow();
resultWindow.Show();
this.Close();
}
ResultWindow.xaml
<TreeView Grid.Row="2" ItemsSource="{Binding}">
<TreeViewItem Header="Review">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="{Binding number}"></Label>
<Label Grid.Column="1" Content="{Binding problem}"></Label>
<RadioButton Grid.Row="1" Content="{Binding choice1}"></RadioButton>
<RadioButton Grid.Row="2" Content="{Binding choice2}"></RadioButton>
<RadioButton Grid.Row="1" Grid.Column="1" Content="{Binding choice3}"></RadioButton>
<RadioButton Grid.Row="2" Grid.Column="1" Content="{Binding choice4}"></RadioButton>
</Grid>
</TreeViewItem>
</TreeView>
答案 0 :(得分:1)
不确定这是否是最佳解决方案,但您可以传递列表&lt;&gt;进入结果窗口的构造函数
在QuizWindow.xaml.cs
中private void btn_submit_Click(object sender, RoutedEventArgs e)
{
ResultWindow resultWindow = new ResultWindow(/*pass list in here*/);
resultWindow.Show();
this.Close();
}
ResultWindow.xaml.cs中的(伪代码)
class ResultsWindow : Window
{
public ResultWindow(list<> answers)
{
InitializeComponent();
//note: in resultwindow.xaml add name = "some_name" to treeview
some_name.datacontext = answers_list;
}
修改强>
MainWindow.xaml中的
<Window x:Class="passlist.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<Button Content="pass" Click="Button_Click"/>
<ListBox Name="listbox1" ItemsSource="{Binding}"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs中的
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<string> strings;
public MainWindow()
{
InitializeComponent();
strings = new List<string>();
strings.Add("this");
strings.Add("that");
listbox1.DataContext = strings;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
passto pt = new passto(strings);
if (!pt.ShowDialog() ?? false)
{
MessageBox.Show("woohooo");
}
}
}
在passto.xaml
中<Window x:Class="passlist.passto"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="passto" Height="300" Width="300">
<Grid>
<ListBox Name="passlist" ItemsSource="{Binding}"/>
</Grid>
</Window>
在passto.xaml.cs
中/// <summary>
/// Interaction logic for passto.xaml
/// </summary>
public partial class passto : Window
{
public passto(List<string> plist)
{
InitializeComponent();
passlist.DataContext = plist;
}
}
此代码直接从我用来测试答案的项目中复制。
结束编辑