首页 > 解决方案 > 如果包含一些字符串,则隐藏 ComboboxItem

问题描述

如果包含一些特定的字符串,有没有办法折叠或隐藏一些 ComboBoxItems?

我在尝试

foreach (ComboBoxItem b in cboServers.Items)
{
      if (b.Content.ToString().Contains("prod")) 
      { b.Visibility = Visibility.Visible; } 
      else 
      { b.Visibility = Visibility.Collapsed; }
}

但不起作用,因为 ComboBoxItem 无法转换为字符串

//Read file's content. 
foreach (IniSection ContainerForThisServer in iniFile.Sections) 
{ 
    cboServidores.Items.Add(ContainerForThisServer.Name.ToUpper()); 
    FillTab(ContainerForThisServer.Name.ToUpper(), ContainerForThisServer); 
}

标签: c#wpfcombobox

解决方案


您的代码不起作用,因为您正在添加大写项目,并且您的 if 语句检查小写“prod”,但为什么不阻止项目进入组合框?

foreach (IniSection ContainerForThisServer in iniFile.Sections) 
{ 
    if (b.Content.ToString().Contains("prod"))
    {
        cboServidores.Items.Add(ContainerForThisServer.Name.ToUpper()); 
        FillTab(ContainerForThisServer.Name.ToUpper(), ContainerForThisServer); 
    }
}

推荐阅读