如果包含某些特定字符串,是否可以折叠或隐藏某些ComboBoxItem?
我正在尝试
foreach (ComboBoxItem b in cboServers.Items)
{
if (b.Content.ToString().Contains("prod"))
{ b.Visibility = Visibility.Visible; }
else
{ b.Visibility = Visibility.Collapsed; }
}
但不起作用,因为ComboBoxItem无法转换为String
//Read file's content.
foreach (IniSection ContainerForThisServer in iniFile.Sections)
{
cboServidores.Items.Add(ContainerForThisServer.Name.ToUpper());
FillTab(ContainerForThisServer.Name.ToUpper(), ContainerForThisServer);
}
答案 0 :(得分:1)
您的代码不起作用,因为您要添加大写字母的项目,而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);
}
}
答案 1 :(得分:0)
List<ComboBoxItem> hiddenItems = new List<ComboBoxItem>();
foreach (ComboBoxItem b in cboServers.Items)
{
if (b.Text.Contains("PROD"))
{
hiddenItems.Add(b);
}
}
foreach (ComboBoxItem hiddenItem in hiddenItems)
{
cboServers.Items.Remove(hiddenItem);
}
//hiddenItems now contains everything that was hidden if you want to use it at a later date
希望有帮助吗?
答案 2 :(得分:0)
我找到了解决方法。
我必须使用:
foreach (IniSection ContainerForThisServer in iniFile.Sections) {
ComboBoxItem i = new ComboBoxItem();
i.Content = ContainerForThisServer.Name.ToUpper();
cboServers.Items.Add(i);
}
代替:
foreach (IniSection ContainerForThisServer in iniFile.Sections) { cboServers.Items.Add(ContainerForThisServer.Name.ToUpper());
}
,现在无需使用.Remove()即可完美实现可见性和折叠状态
foreach (ComboBoxItem b in cboServers.Items)
{
if (b.Content.ToString().ToLower().Contains("prod"))
{ b.Visibility = Visibility.Visible; }
else
{ b.Visibility = Visibility.Collapsed; }
}