C# - how do I load different listviewitems when selecting different ComboBoxItems?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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(); } |
我想这样做,以便在单击第一个组合框项目时将第一个数组中的字符串添加到列表视图中。但是由于没有任何事情发生,所以缺少了一些东西。 (Items.Clear仅用于测试,我还将使用它来清除我在WPF中定义的以前的listviewitem)。在此先感谢!
您可以将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs 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; switch (izbranIndex) { case 1: lvDataBinding.ItemsSource = izbira1; break; case 2: lvDataBinding.ItemsSource = izbira2; break; case 3: lvDataBinding.ItemsSource = izbira3; break; case 4: lvDataBinding.ItemsSource = izbira4; break; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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)); } } } |
尝试使用
1 2 3 4 5 | if(izbranIndex == 1){ foreach (string item in izbira1){ listview.Items.Add(item); } } |