2012-10-11 69 views
0

我试图将ListItems添加到ListBox(ListBox3),这取决于另一个ListBox(ListBox1)的selectedItem。问题是,这些项目不会添加到列表框中。WP7:根据来自另一个列表框的输入填充列表框

下面的代码:

 private void createlist() 
     { 
     if (listBox1.SelectedValue.ToString().Equals("EPL")) 

     { 
      ListBoxItem manchesterunited = new ListBoxItem(); 
      manchesterunited.Content = "Manchester United"; 
      listBox3.Items.Add(manchesterunited); 
     } 
    } 

    private void listBox1_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) 
    { 
     createlist(); 
    } 

createlist()做变化,被称为在ListBox1中的SelctionChanged()事件。

新来C#和WP7编程,任何帮助将不胜感激。

回答

0

在您的viewmodel中创建列表,并在您的视图模型SelectedList中将listbox绑定到list<>。当用户从ListBox1中选择项目时,只需使用适当的列表和Notify the property changed event更改SelectedList的值即可。这将会完成。!

0

我认为你的程序不能运行在mvvm结构中。

确保你的逻辑是正确的。你可以在该行创建一个断点
ListBoxItem manchesterunited = new ListBoxItem(); 确保在代码块中运行这些代码。

在列表框中添加控件的方式是正确的。