该项目打开一个对话框,让用户选择课程。它现在产生的输出只给我一个可供选择的课程。如何让课程班显示所有课程?
这是mainWindow文件:
using System;
using System.Collections.Generic;
using System.Linq;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CreateClassesObjs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Course choice;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Course course1 = new Course();
Course course2 = new Course();
Course course3 = new Course();
Course course4 = new Course();
Course course5 = new Course();
Course course6 = new Course();
Course course7 = new Course();
course1.setName("IT 145");
course2.setName("IT 200");
course3.setName("IT 201");
course4.setName("IT 270");
course5.setName("IT 315");
course6.setName("IT 328");
course7.setName("IT 330");
this.comboBox.Items.Add(course1);
this.comboBox.Items.Add(course2);
this.comboBox.Items.Add(course3);
this.comboBox.Items.Add(course4);
this.comboBox.Items.Add(course5);
this.comboBox.Items.Add(course6);
this.comboBox.Items.Add(course7);
}
private void button_Click(object sender, RoutedEventArgs e)
{
choice = (Course)(this.comboBox.SelectedItem);
this.listBox.Items.Add(choice);
}
}
}
这是我的档案。
namespace CreateClassesObjs
{
internal class Course
{
static string name;
public Course()
{
}
public string setName(string choice)
{
name = choice;
return name.ToString();
}
/*
public string retrieveName(string choice)
{
name = choice;
return name.setName();
}
*/
public override string ToString()
{
return name;
}
}
}
它只打印最后一个值。什么时候应该打印所有这些;
course1.setName("IT 145");
course2.setName("IT 200");
course3.setName("IT 201");
course4.setName("IT 270");
course5.setName("IT 315");
course6.setName("IT 328");
course7.setName("IT 330");
ToString应该可以获得所有以前的字符串。
答案 0 :(得分:1)
简短/快速回答(坚持代码背后):
ComboBox仅支持选择单个项目。 通过将其替换为ListBox,您可以将其配置为接受多个选定项。这些在SelectedItems属性中可用。
private void button_Click(object sender, RoutedEventArgs e)
{
foreach(var choice in this.listBoxSource.SelectedItems.OfType<Course>())
{
this.listBox.Items.Add(choice);
}
}
长答案(走MVVM方式):
在Course类上实现INotifyPropertyChanged以支持正确的数据绑定。
将Selected属性添加到Course类。 将DataTemplate添加到Source列表框,该列表框显示包含Selected Property的复选框的课程。
这将允许您使用数据绑定,因为您现在可以检查课程本身以查明它们是否被选中。这导致UI背后的代码更少,这使得更容易进行更改,维护和(单元)测试。
答案 1 :(得分:0)
以下是我能够使该计划有效的方法。
using System;
using System.Collections.Generic;
using System.Linq;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CreateClassesObjs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Course choice;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Course course1 = new Course("IT 145");
Course course2 = new Course("IT 200");
Course course3 = new Course("IT 201");
Course course4 = new Course("IT 270");
Course course5 = new Course("IT 315");
Course course6 = new Course("IT 328");
Course course7 = new Course("IT 330");
course1.setName("IT 145");
course2.setName("IT 200");
course3.setName("IT 201");
course4.setName("IT 270");
course5.setName("IT 315");
course6.setName("IT 328");
course7.setName("IT 330");
this.comboBox.Items.Add(course1);
this.comboBox.Items.Add(course2);
this.comboBox.Items.Add(course3);
this.comboBox.Items.Add(course4);
this.comboBox.Items.Add(course5);
this.comboBox.Items.Add(course6);
this.comboBox.Items.Add(course7);
}
private void button_Click(object sender, RoutedEventArgs e)
{
choice = (Course)(this.comboBox.SelectedItem);
this.listBox.Items.Add(choice);
}
class Course
{
private string name = "";
public Course(string name)
{
this.name = name;
}
public void setName(string name)
{
this.name = name;
}
public string getName()
{
return name;
}
public override string ToString()
{
return getName();
}
}
}
}