C# - 如何在选择不同的ComboBoxItem时加载不同的listviewitems?

时间:2017-03-26 11:52:36

标签: c# wpf listview

private void cbKlik_GotFocus(object sender, RoutedEventArgs e)
{
    string[] izbira1 = { "Kingston 2, 5'' SSD disk 480 GB, SATA3", "DELL monitor LED UltraSharp U2412M", "Lenovo IdeaPad 110" };
    string[] izbira2 = { "PCX namizni računalnik Exam i5-7400/8GB/SSD120+1TB/Win10H", "Lenovo prenosnik V310", "Intel procesor Core i7-5820K" };
    string[] izbira3 = { "HP prenosnik Pavilion 17-ab004nm", "Intel procesor Core i7 6900K", "Gigabyte grafična kartica GTX 1080 OC" };
    string[] izbira4 = { "Asus prenosnik FX502VM-DM311T", "HP prenosnik Omen 17-w103nm", "DELL prenosnik Alienware 17" };

    ComboBox cmb = (ComboBox)sender;
    int izbranIndex = cmb.SelectedIndex;

    if (izbranIndex == 1)
    {
        lvDataBinding.Items.Clear();
    }
    //lvDataBinding.Items.Clear();

}

我想这样做,当我点击第一个组合框时,第一个数组中的字符串会被添加到listview中。但由于没有发生任何事情,因此缺少某些东西。 (Items.Clear仅用于测试,我也将使用它来清除我在WPF中定义的先前listviewitems)。提前谢谢!

3 个答案:

答案 0 :(得分:0)

尝试使用SelectionChanged事件;然后遍历数组并将项添加到列表视图中。

if(izbranIndex == 1){
foreach (string item in izbira1){ 
   listview.Items.Add(item);
   }
}

答案 1 :(得分:0)

ComboBox cmb = (ComboBox)sender;
cmb.SelectionChanged += cmbx_SelectionChanged;

void cmbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show(string.Format("Index has been changed {0}", yourcomobobox.SelectedIndex));
 if(yourcomobobox.SelectedIndex==1)
  {
   listview.Items.Clear();
   foreach (string item in izbira1){ 

   listview.Items.Add(new ListViewItem(item));
   }
  }
}

答案 2 :(得分:0)

您可以将ItemsSource的{​​{1}}属性设置为ListView。尝试处理ComboBox的string[]事件,如下所示:

SelectionChanged