【发布时间】:2016-05-31 10:55:24
【问题描述】:
我在主窗口类中有这段代码,我在其中声明了一些值以放入ListForTesting 列表中,并使测试出现在组合框中。稍后有另一个组合框取决于第一个组合框,我在下面向您展示代码:
值得一提的是,我对 C# 完全陌生。仅在 VBA 中编码。我是一名机械工程师,不是软件 :)
主窗口代码
public MainWindow()
{
InitializeComponent();
grid_projectConfig.Visibility = Visibility.Collapsed;
grid_projectOverview.Visibility = Visibility.Collapsed;
grid_test.Visibility = Visibility.Collapsed;
grid_reports.Visibility = Visibility.Collapsed;
//ListForTesting holds the hardcoded set of tests and manoeuvres
List<HelpClass.TestID> ListForTesting = new List<HelpClass.TestID>();
//TestObject holds the test name, ID and all the manoeuvres related to it
HelpClass.TestID TestObject = new HelpClass.TestID();
TestObject.testName = "Steady State";
TestObject.ID = 0;
//Manoeuvre holds the manoeuvre name and its ID
TestObject.Manoeuvres = new List<HelpClass.ManoeuvresID>();
HelpClass.ManoeuvresID Manoeuvre = new HelpClass.ManoeuvresID();
Manoeuvre.manName = "30 kph";
Manoeuvre.manID = 0;
//add the Manoeuvre to the TestObject
TestObject.Manoeuvres.Add(Manoeuvre);
//create new Manoeuvre
Manoeuvre = new HelpClass.ManoeuvresID();
Manoeuvre.manName = "50 kph";
Manoeuvre.manID = 1;
TestObject.Manoeuvres.Add(Manoeuvre);
//add the TestObject to the ListForTesting
ListForTesting.Add(TestObject);
//display the tests in a combobox
combobox_testType.ItemsSource = ListForTesting.Select(t => t.testName);
}
第二个组合框代码
public void combobox_testType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
combobox_testType.ItemsSource = ListForTesting[1].Manoeuvres.Select(t => t.manName);
}
最后一行代码不起作用,因为它告诉我它在当前上下文中不存在。
【问题讨论】:
-
有什么问题?