【问题标题】:C# - how do I load different listviewitems when selecting different ComboBoxItems?C# - 选择不同的 ComboBoxItems 时如何加载不同的列表视图项?
【发布时间】:2017-03-27 03:20:35
【问题描述】:
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 中定义的以前的 listviewitems)。提前致谢!

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    您可以将ListViewItemsSource 属性设置为string[]。尝试为 ComboBox 处理 SelectionChanged 事件,如下所示:

    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;
        }
    }
    

    【讨论】:

    • 您的解决方案有效,但不幸的是仅在极少数情况下有效。我需要在插入新的 l​​istviewitems 之前清除列表,我使用 lvDataBinding.Items.Clear();选择第二个选项后我的应用程序崩溃(可能是因为我清除了一个已经为空的列表)。我该如何解决这个问题?
    • 您应该通过将 ItemsSource 属性设置为 null 来清除 ListView:lvDataBinding.ItemsSource = null;
    • 有效!谢谢!但我还有另一个问题。我以前拥有的 ListViewItems(我在 WPF 中制作它们)具有显示 ListViewitem 名称的 onclick MessageBox。现在我用 ComboBox 加载新的并单击它们,当然它会显示以前的 listviewitems 的名称。我需要定义一个保存列表视图项目名称的数据结构。我该怎么做?
    • 如果您还有其他问题,请提出其他问题。
    【解决方案2】:
    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));
       }
      }
    }
    

    【讨论】:

    • 为您更新了代码。希望它有效。如果对您有帮助,请将其标记为答案
    • pastebin.com/RNbUJF7X 如果我理解正确,这就是我使用您的解决方案的方式。但问题是当我打开 MainWindow 并单击第一个选项时没有任何反应。单击任何其他选项后,项目将添加到列表视图中,因为我可以单击它们但不显示名称。而第一个选项只有在我不先点击的时候才有功能。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 2013-03-06
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多