需要帮助尝试显示我添加到列表框中的值。我研究了几种添加到列表框的方法。这是我的最新尝试,但我无法弄清楚如何显示" Netflix"和" Hulu"。调试后,值在列表框内。我只是看不到文字。
在旁注上我正在研究这个以显示BASIC观察者模式。为了显示代码中的更改,我想显示订阅不同提供程序的结果。先谢谢你们!
public partial class MainWindow : Window
{
List<string> myList = new List<string>();
public MainWindow()
{
InitializeComponent();
}
private void subscribeToNetflixButton_Click(object sender, RoutedEventArgs e)
{
Netflix netflix = new Netflix("Stir Crazy");
Observer subscriberOne = new Observer();
netflix.AddObserver(subscriberOne);
myList.Add("Netflix");
listBox.Items.Add(myList.ToArray());
}
private void subscribeToHuluButton_Click(object sender, RoutedEventArgs e)
{
Hulu hulu = new Hulu("Willy Wonka and the Chocolate Factory");
Observer subscriberTwo = new Observer();
hulu.AddObserver(subscriberTwo);
myList.Add("Hulu");
listBox.Items.Add(myList.ToArray());
}
}
答案 0 :(得分:0)
您应该将AddRange与不属于Add
的集合一起使用:
public partial class MainWindow : Window
{
List<string> myList = new List<string>();
public MainWindow()
{
InitializeComponent();
}
private void subscribeToNetflixButton_Click(object sender, RoutedEventArgs e)
{
Netflix netflix = new Netflix("Stir Crazy");
Observer subscriberOne = new Observer();
netflix.AddObserver(subscriberOne);
myList.Add("Netflix");
listBox.Items.AddRange(myList.ToArray());//This line should be changed
}
private void subscribeToHuluButton_Click(object sender, RoutedEventArgs e)
{
Hulu hulu = new Hulu("Willy Wonka and the Chocolate Factory");
Observer subscriberTwo = new Observer();
hulu.AddObserver(subscriberTwo);
myList.Add("Hulu");
listBox.Items.AddRange(myList.ToArray());//And this line
}
}
顺便说一句:如果您使用WPF,虽然您必须添加自己的AddRange(默认情况下不可用),例如,通过在扩展方法内循环收集,如果您不在,则在外面循环。多次需要这个功能。